AlarmManager警报未被删除/取消。我究竟做错了什么?

时间:2012-09-28 04:08:58

标签: android alarmmanager

我正在使用AlarmManager + NotificationManager在特定时间显示通知。但是,我无法通过我的代码取消警报。在我删除警报后,通知/警报仍然被触发。有人可以查看我的代码并检查我是否做错了什么?谢谢SOOOOO! :)

这就是我设置闹钟/通知的方式:

Intent intent = new Intent(this, ViewLocalReminders.class);
startActivity(intent);

//Get the primary key of the reminder that has just been saved.
Cursor cursor = remindersDAO.reminderNotification(this);
cursor.moveToLast();
int reminderIdColumnIndex = cursor.getColumnIndex("_id");
reminderPrimaryKey = cursor.getInt(reminderIdColumnIndex);

//Get the name of the reminder that has just been saved.
cursor.moveToLast();
int reminderNameColumnIndex = cursor.getColumnIndex("name");
reminderName = cursor.getString(reminderNameColumnIndex);

cursor.close();

Toast toast = Toast.makeText(context, context.getString(R.string.reminder_saved), Toast.LENGTH_LONG);
toast.show();

c.set(mYear, mMonth, mDay); //Set the notification date.
c.set(Calendar.HOUR_OF_DAY, pHour); //Set the notification hour.
c.set(Calendar.MINUTE, pMinute); //Set the notification minute.
c.set(Calendar.SECOND, 0); //Set the notification second (always 0).

//Use AlarmManager to trigger the notification/alarm.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

//PendingIntent to launch activity when the alarm triggers.                    
Intent i = new Intent("com.utilityapps.Blah.DisplayReminderNotification");

//Assign the reminder's primary key as the notification ID.
i.putExtra("Reminder_Name", editRemindMeTo.getText().toString());
i.putExtra("Reminder_Primary_Key", reminderPrimaryKey);

PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), reminderPrimaryKey, i, 0);               

//Set the alarm to trigger.
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), displayIntent);

这就是我取消闹钟/通知的方式:

Intent i = new Intent("com.utilityapps.Blah.DisplayReminderNotification");
PendingIntent displayIntent = PendingIntent.getService(context, (int) reminderID, i, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

alarmManager.cancel(displayIntent);
displayIntent.cancel();

因此,我需要在代码中更改某些内容才能使其正常工作吗?谢谢! :d

1 个答案:

答案 0 :(得分:4)

设置和取消警报时,您的未决意图不同。为两者创建相同的待处理意图。希望它可以正常工作。

取消闹钟

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Intent i = new Intent("com.utilityapps.Blah.DisplayReminderNotification");

PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), reminderPrimaryKey, i, 0);               

alarmManager.cancel(displayIntent);