2014-07-05 36 views
0

我正在開發一個Android項目,只是想在特定的時間每天都有一個警報。爲了這個目的,我在一項服務中使用了alarmmanger。添加我的懸而未決的意圖alarmmanger並設置我的時間就像如下:alarmmanager觸發待定的意圖不準時

Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, h); 
    calendar.set(Calendar.MINUTE, m); 

    Intent i = new Intent(DefaultActivity.fullname(Actions.TIME_CHANGED)); 

    i.putExtra(TIME_NAME, name); 
    i.putExtra(TIME_CLOCK, clock); 


    PendingIntent pendingIntent = PendingIntent.getService(ctx, index, i, PendingIntent.FLAG_UPDATE_CURRENT); 

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

問題是當過我打電話給我的服務,我得到了「Actions.TIME_CHANGED」

我的啓動命令是這樣的:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
String intentAction = intent == null ? null : intent.getAction(); 

if (intentAction != null) 
{ 
    Log.e(MainActivity.WATERMELON_TAG, "action: " + intentAction); 

    if (intentAction.equals(DefaultActivity.fullname(Actions.TIME_CHANGED))) 
    { 
     clockIsChanged(intent); 
    } 

    if (intentAction.equals(DefaultActivity.fullname(Actions.TIME_RENEW_CHECKING))) 
    { 
     reNew(); 
    } 

    if (intentAction.equals(DefaultActivity.fullname(Actions.FIRST_TIME_BOOT))) 
    { 
     reNew(); 
    } 
} 

return START_STICKY; 
} 

我不知道是什麼問題。我的解決方案是我添加一個函數來檢查pendingintent時間是否等於當前時間執行我的任務,但它不是一個好的解決方案。

爲什麼會發生這種情況?

在此先感謝

+0

份額轉儲有錯誤Actions.TIME_CHANGED – ajay

+0

我看到其先前的問題也被提出在yesreday http://stackoverflow.com/questions/24573854/alarmmanager -repeating-intent-does-not-trigger-right .......建議在https://developer.android.com/training/scheduling/alarms.html查看RTC示例 – ajay

+0

我像開發人員一樣實現我的代碼現場!但我不知道爲什麼每次都會觸發我的意圖? – strings95

回答

2

文檔說:

注:爲API 19日,所有重複報警是不準確的。

因此,如果您使用的是19 API更高,你應該更好地利用AlarmManager.setExact和resceduel在onReceive - 方法報警。

另一個posibility使用的API級別低於19

+1

但他們爲什麼每次都打電話? – strings95