取消具有唯一RequestCode的PendingIntent



我正试图取消一个pendingIntent,所以总是只有一个。我的requestCode是"1"或"2",这取决于服务是启动还是停止。启动警报的PendingIntent如下(停止几乎相同):

Intent i = new Intent(context, Service.class);
i.setAction("start");
PendingIntent pi = PendingIntent.getService(context, SaveSchedulePrefs.getStartReqCode(context), i, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pi)

新的pendingIntent看起来是这样的:

alarmManager.setExact(AlarmManager.RTC_WAKEUP, mTime, pi);

根据我所读到的内容,pendingIntent必须完全相同才能被取消,requestCode为1应该满足这一需求。这实际上可能按预期工作,但警报重新触发的多天日志文件显示了以下两个启动/停止警报(每次发生7次):

RTC_WAKEUP #0: Alarm{b216a768 type 0 com.myapp.myRinger}
operation=PendingIntent{b2169318: PendingIntentRecord{b20e5820 com.myapp.myRinger startService}}
  RTC_WAKEUP #0: Alarm{b2172ed0 type 0 com.myapp.myRinger}
operation=PendingIntent{b2172ec0: PendingIntentRecord{b20e6160 com.myapp.myRinger startService}}
  com.myapp.myRinger +856ms running, 14 wakeups:
+759ms 7 wakes 7 alarms: act=stop cmp={com.myapp.myRinger/com.myapp.myRinger.Service}
+758ms 7 wakes 7 alarms: act=start cmp={com.myapp.myRinger/com.myapp.myRinger.Service}

我在日志中看到的是已经激活的警报的历史记录,还是没有一个警报被取消?

看起来您的原始PendingIntent具有ACTION="start"或ACTION="stop"。在您发布的代码中,PendingIntent不包含ACTION。在这种情况下,它将不匹配(因此不会取消警报)。

为了使PendingIntent匹配,以下内容必须匹配:ACTION、DATA、COMPONENT。意向附加项不会进行比较,出于匹配目的会被忽略。

最新更新