【问题标题】:Android how to cancel AlarmManager.setAlarmClock()Android如何取消AlarmManager.setAlarmClock()
【发布时间】: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


【解决方案1】:

PendingIntent.FLAG_CANCEL_CURRENT 表示“取消当前的PendingIntent”。当您转到cancel() 时,这会使AlarmManager 感到困惑,而困惑的AlarmManager 绝不是一件好事。

只有在您有明确的理由时才使用PendingIntent.FLAG_CANCEL_CURRENT。不要将它用作一些默认值,“哦,我将使用它而不是 0,因为我喜欢输入很多字符”值。 :-)

【讨论】:

  • 此外,为这两个目的使用相同的 PendingIntent sender 不太可能正常工作。 AlarmClockInfo 构造函数的showIntent 参数告诉操作系统当用户点击通知抽屉或锁定屏幕中的闹钟信息时要做什么。 setAlarmClock()operation 参数告诉 AlarmManager 当警报“响起”时该做什么。大概你应该将后者传递给cancel()
  • @CommonsWare,PendingIntent.FLAG_NO_CREATEAlarmManager.AlarmClockInfosetAlarmClock 都有效吗?
  • @Vyacheslav: FLAG_NO_CREATEAlarmManager 中的任何内容都没有任何关系。我建议你问一个新的 Stack Overflow 问题,在那里你可以准确地解释你关心的问题。
  • @CommonsWare ,我已经创建了它:stackoverflow.com/questions/36110837/…
猜你喜欢
  • 1970-01-01
  • 2014-02-12
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多