我有一個家庭作業計劃器應用程序,我想創建一個鬧鐘,提醒用戶在某個特定時間前/晚的作業。我有一個名爲AddNewHomework的活動,這是用戶創建一個新的作業項目並將其添加到數據庫的位置。這個代碼然後被調用。Android AlarmManager - 從其他活動取消
Intent i = new Intent(this, AlarmNotificationReceiver.class);
i.putExtra("title", title);
PendingIntent pi = PendingIntent.getBroadcast(this.getApplicationContext(), (int) id, i, 0);
AlarmManager mAlarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mAlarm.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 10 * 1000, pi);
這會創建一個新的鬧鐘,它會在10秒內觸發並創建通知。
我的問題是,我有另一個名爲HomeworkList的活動,它包含一個ListView並顯示存儲在數據庫中的所有Homeworks。如果用戶長時間點擊一個,那麼他們可以選擇將其刪除,但是將其從數據庫中刪除將不會刪除該警報。
我已經查找了可用於刪除鬧鐘的代碼,但是我還沒有找到任何可以讓我瞭解如何從這個不同的活動中取消鬧鐘的代碼。我知道我需要使PendingIntent相同,但是我該如何做到這一點,因爲我無法訪問其他類的上下文。 (我不認爲)。
您已嘗試'mAlarm.cancel(PendingIntent);'取消警報管理器? – 2012-04-07 15:02:30
@imrankhan我有,但是我需要一個相同的PendingIntent來找到鬧鐘並取消它。我的問題是創建相同的PendingIntent的 – TomRichardson 2012-04-07 15:32:27