首先,本網站很棒,每個人都非常有幫助。這是我的第一篇文章,所以原諒我,如果我有任何ommited。Android AlarmService重複警報在取消呼叫後重復重複一次
我創建警報像這樣:
private void startLocation() {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyWeatherUpdateService.class);
PendingIntent service = PendingIntent.getService(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
alarm.setRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() + (60 * 1000),
Long.parseLong(prefs.getString("listpref", "60000")), service);
}
在該方法中其被稱爲片段內,上下文是從getApplication(),listpref是以毫秒爲單位的字符串更新間隔。
我通過取消:
public void endLocation() {
Intent i = new Intent(context, MyWeatherUpdateService.class);
PendingIntent service = PendingIntent.getService(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
alarm.cancel(service);
}
確保意圖/未決意圖是相同的。
現在我有2個問題:
1)火警幾乎imediately創建後,即使我告訴它1分鐘後開始。 2)當我呼叫取消時,警報在取消之前再次發生。
問題1)爲什麼鬧鐘會很快起火?和2)這是否按預期工作,或者如果報警立即取消,就像我想要的那樣。
如果我沒有提供足夠的信息,如果需要添加更多的代碼。
在此先感謝。
謝謝您的及時答覆,但我自己排序。我實際上調用了兩次startlocation方法。抱歉。 – yrag69
我應該接受你的答案還是離開? – yrag69
@CommonsWare感謝!!,defintely upvote這個好答案 –