Android 如何在设置新闹钟的同时不取消之前的闹钟

Android how to not cancel previous alarm while setting new one

好的,就像在AlarmManager.set中一样:

If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled.

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

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

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

解决方法是在发送之前了解 AlarmManager 是否已经安排了未决意图。

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

好的,知道了。

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

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

当我尝试设置新的 Alarm 时 - 首先我会检查 - 这个 PendingIntent 是否存在。
而且,当 Alarm 触发时 - 我通过调用 cancel().

手动取消 myPendingIntent

效果不错,如我所愿。