具有不同上下文的 PendingIntent FLAG_NO_CREATE

PendingIntent FLAG_NO_CREATE with different Context

好的,我有下一个方法:

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

myIntent总是一样的。

我想知道 - 如果我将不同的 context 传递给此方法,它 returns false.

但是如果我计划新 Alarm 有:

PendingIntent.getService(context, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);

(也有不同的context)-它会清除之前的警报(因为myIntent是相同的,这就是为什么PendingIntent考虑相同)。

为什么 context 会有不同的行为?
有没有办法忽略我的isPresentPengingIntent方法中的context参数?

Context不是你的问题。您看到的是 PendingIntent.FLAG_ONE_SHOT 工作方式的奇怪之处。

如果您使用 FLAG_ONE_SHOT 创建了 PendingIntent,则无法使用您的代码确定 PendingIntent 是否存在。就Android而言,这个PendingIntent是不存在的

如果要以PendingIntent的存在为指标来判断是否设置闹钟,则不能使用FLAG_ONE_SHOT.