【发布时间】:2014-10-17 22:53:22
【问题描述】:
API 21 中有这个new AlarmManager.setAlarmClock(...) method,它设置一个新的警报并显示一个状态栏警报图标。
我是这样使用的:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_ALERT_ACTION);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setAlarmClock(new AlarmManager.AlarmClockInfo(time, sender), sender);
问题是我不知道如何取消这个警报,因为这段代码不起作用:
am.cancel(PendingIntent.getBroadcast(context, 0, new Intent(ALARM_ALERT_ACTION), PendingIntent.FLAG_CANCEL_CURRENT));
警报本身被取消(我的 BroadcastReceiver 的 onReceive 没有被调用)但状态栏警报图标仍然显示并且 AlarmManager.getNextAlarmClock() 返回此警报。
【问题讨论】:
-
尝试从您的
cancel()通话中删除PendingIntent.FLAG_CANCEL_CURRENT。 -
@CommonsWare 好用 :) 非常感谢!
-
@CommonsWare @Smuggler 如何删除
PendingIntent.FLAG_CANCEL_CURRENT,它不是可选参数。 -
@AVEbrahimi:将其替换为
0。
标签: android alarmmanager android-alarms android-5.0-lollipop