【发布时间】:2014-02-26 21:03:55
【问题描述】:
背景:
我正在通过 AlarmManager 使用 PendingIntent 进行警报。
问题:
一开始我以为要取消之前的,我必须提供我之前用来启动警报的确切 requestCode。
但后来我发现我错了,正如cancellation API 所说:
删除任何具有匹配 Intent 的警报。任何警报,任何类型, 其 Intent 与此匹配(由 filterEquals(Intent) 定义), 将被取消。
看着“filterEquals”,文档说:
根据意图的目的确定两个意图是否相同 分辨率(过滤)。也就是说,如果他们的动作、数据、类型、类、 和类别是一样的。这不会比较任何额外的数据 包含在意图中。
所以我不明白“requestCode”的用途......
问题:
“requestCode”是做什么用的?
如果我使用相同的“requestCode”创建多个警报怎么办?它们会相互覆盖吗?
【问题讨论】:
-
如果你使用相同的 requestCode 你会得到相同的 PendingIntent
-
对于 PendingIntent.getBroadcast(),Android 显然忽略了 requestCode。从 API 22 开始,它不会使您的 Pending Intent 独一无二。用于 getActivity() (可能还有 getService() 但我没有测试过)。 stackoverflow.com/a/33203752/2301224
-
@Baker 这不被认为是一个错误吗?如果这是一个错误,你应该在这里写下它:code.google.com/p/android/issues/list
-
嗯,实际上,文档确实指定了 requestiCode 的用法:
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent#filterEquals(Intent), or different request code integers supplied. -
@Eir 对,那么使用 requestCode 有什么意义呢?可以在哪里使用?
标签: android android-intent alarmmanager android-pendingintent