讓我們看看是否有人發現這個問題,並可以幫助我解決這個難題。我有以下情況。該應用程序是一個簡單的待辦事項列表:AlarmManager無法取消鬧鐘
public class NewToDo extends Activity {
private PendingIntent pendingIntent;
private AlarmManager manager;
...
當用戶創建待辦事項與通知,我在這個類的這樣的方法之一創建:
int interval = 30000;
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
/*Let's let the putExtras apart, as documentation says they're not considered when comparing intents, but I add some of them*/
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(), _id, alarmIntent, alarmIntent.FILL_IN_DATA);
/*Printing _id we see 3, for instance, but is a unique identifier to associate the notification with this to-do item*/
/*I use the flag alarmIntent.FILL_IN_DATA because I want to send extras to create a custom notification*/
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + interval, interval,
pendingIntent);
我退出了應用程序(但是如果我沒有退出則沒有區別)。然後通知每30秒正確到達一次,然後選擇應用程序在該項目的詳細視圖中啓動。一個操作欄按鈕允許編輯它,再次調用同一個類NewToDo。 雖然編輯工作,我可以取消與待辦事項相關的通知和我的代碼做的是這樣的 - 在NewToDo講座:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class); //creating a matching Intent
Log.v(getString(R.string.app_name), "_id: " + _id); //the 3 aforementioned
PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), _id, i, alarmIntent.FILL_IN_DATA);
alarmManager.cancel(displayIntent);
但通知繼續出現每次30秒,所以他們不取消。
我看到另一篇文章提出以下建議:
「意圖intentYouWantToCancel =新意圖(這一點,AlarmReceive.class); intentYouWantToCancel.setAction(」 aRelevantString「);
當你設置警報管理器以及何時刪除它。「
因此,我將兩種意圖都作爲'aRelevantString'強制轉換爲唯一標識符的字符串。但同樣的問題。
也改變了getBaseContext()爲這個,以防萬一,但沒有區別。
沒有錯誤,只是通知不斷重複忽略我的取消。任何想法? PS:我每次運行AVD時都會重新啓動AVD。我意識到,我使用filterEquals的打印方式根本沒有意義(這個時間太長),但這並不影響問題的其餘部分。
謝謝回答!這個確切的代碼並沒有解決問題,但它幫助我重新考慮爲什麼我使用PendingIntent.getActivity而不是PendingIntent.getBroadcast,並最終讓我找到了解決方案。我將把它粘貼在答案中。 – 2015-04-03 13:59:48
酷..開心編碼。 – Keshav1234 2015-04-03 14:04:37