取消预定的Android通知

时间:2016-02-11 14:14:45

标签: android alarmmanager android-pendingintent

我已经使用闹钟管理器安排了android通知但我无法取消通知,我尝试使用此notificationManager.cancelAll();取消它不起作用然后我试图取消闹钟管理器本身,不幸的是,不起作用。

有谁知道如何取消预定通知?

这就是我为通知设置警报管理器的方法:

Intent myIntent = new Intent(DirctActivityfinal3.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(DirctActivityfinal3.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent);

我尝试使用此代码取消警报管理器,但无法正常工作我仍然收到通知

Intent intent = new Intent(ViewTicketActivity.this, DeleteNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this,0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
alarmManager.cancel(pendingIntent);

2 个答案:

答案 0 :(得分:5)

你的PendingIntents不一样。

要取消警报,您为取消警报而创建的待处理意图必须与您创建警报的意图相同。

e,在你的例子中,你将不同的类传递给每个。

Intent myIntent = new Intent(DirctActivityfinal3.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(DirctActivityfinal3.this, 0, myIntent, 0);

不同
Intent intent = new Intent(ViewTicketActivity.this, DeleteNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this,0, intent, 0);

完成示例:

Intent myIntent = new Intent(myContext, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(myContext, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

然后

alarmManager.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent);

alarmManager.cancel(pendingIntent);

答案 1 :(得分:0)

将Intent,PendingIntent和AlarmManager声明为全局变量

取消预定的

pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);