【问题标题】:Android how to not cancel previous alarm while setting new oneAndroid如何在设置新警报时不取消以前的警报
【发布时间】:2016-06-24 19:00:45
【问题描述】:

好的,就像在AlarmManager.set:

如果已经为同一个 IntentSender 安排了警报,则将首先取消之前的警报。

如何更改此默认行为?
我只需要设置一次警报。
如果要取消新警报(使用相同的PengingIntent),我需要忽略它们。

所以我需要取消新的闹钟,但之前的闹钟才有效。

【问题讨论】:

    标签: android alarmmanager alarm


    【解决方案1】:

    我不知道这样做的正确方法。

    一种解决方法是在发送之前了解 AlarmManager 是否已经安排了待处理的意图。

    使用SharedPreference 存储描述意图日期的时间戳。在向 AlarmManager 发送另一个 PendingIntent 之前检查时间戳是否已通过。

    【讨论】:

    • 是的,似乎这是唯一的方法。但如果存在的话,我会尝试找到更好的解决方案。
    【解决方案2】:

    好的,知道了。

    我创建了 PendingIntent 没有 FLAG 选项。
    然后我有方法:

    private boolean isPresent() {
        return PendingIntent.getService(context, 0, myIntent, PendingIntent.FLAG_NO_CREATE) != null;
    }
    

    当我尝试设置新的Alarm 时 - 首先我检查一下 - 这个PendingIntent 是否存在。
    而且,当Alarm 触发时 - 我通过调用cancel() 手动取消myPendingIntent。

    效果很好,正如我所愿。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多