有这个new AlarmManager.setAlarmClock(...) method在 API 21 中,它设置一个新的警报并显示一个状态栏警报图标。
我是这样使用的:
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() 返回这个警报。
最佳答案
PendingIntent.FLAG_CANCEL_CURRENT
表示“取消当前的 PendingIntent
”。当您转到 cancel()
时,这会使 AlarmManager
感到困惑,而感到困惑的 AlarmManager
从来都不是一件好事。
仅当您有明确的理由时才使用 PendingIntent.FLAG_CANCEL_CURRENT
。不要将它用作某些默认值,“哦,我将使用它而不是 0
,因为我喜欢输入很多字符”值。 :-)
关于Android如何取消AlarmManager.setAlarmClock(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434490/