Android PendingIntent 上使用的“requestCode”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21526319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:42:31  来源:igfitidea点击:

What's "requestCode" used for on PendingIntent?

androidandroid-intentalarmmanagerandroid-pendingintent

提问by android developer

Background:

背景:

I'm using PendingIntent for alarms via AlarmManager.

我正在通过 AlarmManager 使用 PendingIntent 发出警报。

The problem:

问题:

At first I thought that in order to cancel previous ones, I must provide the exact requestCode that I've used before to start the alarm.

一开始我想,为了取消之前的,我必须提供我之前用过的确切的requestCode来启动警报。

But then I've found out I was wrong, as the cancellation APIsays:

但后来我发现我错了,正如取消 API所说:

Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.

删除任何具有匹配意图的警报。任何类型的任何警报,其意图与此匹配(由 filterEquals(Intent) 定义)都将被取消。

looking at "filterEquals", the documentation says:

查看“ filterEquals”,文档说:

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

出于意图解析(过滤)的目的,确定两个意图是否相同。也就是说,如果它们的动作、数据、类型、类和类别是相同的。这不会比较意图中包含的任何额外数据。

so I don't get what the "requestCode" is for...

所以我不明白“requestCode”是干什么用的……

The question:

问题:

What is "requestCode" used for?

“请求代码”是做什么用的?

What if I create multiple alarms with the same "requestCode" ? do they override each other?

如果我使用相同的“requestCode”创建多个警报怎么办?他们是否相互覆盖?

采纳答案by Minhaj Arfin

  1. requestCodeis used to retrieve the same pending intent instance later on (for cancelling, etc).
  2. Yes, my guess is the alarms will override each other. I would keep the request codes unique.
  1. requestCode用于稍后检索相同的未决意图实例(用于取消等)。
  2. 是的,我的猜测是警报会相互覆盖。我会保持请求代码的唯一性。

回答by HendraWD

I just want to add to @Minhaj Arfin answer

我只想添加到@Minhaj Arfin 的答案中

1- requestCode is used to get the same pending intent later on (for cancelling etc)

1- requestCode 用于稍后获得相同的未决意图(用于取消等)

2- Yes, they will get override as long as your specify the same Receiver to your Intent that you specify on your PendingIntent

2- 是的,只要您为您在 PendingIntent 上指定的 Intent 指定相同的 Receiver,它们就会被覆盖

example:

例子:

Intent startIntent1 = new Intent(context, AlarmReceiverFirst.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, startIntent1, 0);

Intent startIntent2 = new Intent(context, AlarmReceiverSecond.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, 0);

From above example, they will notoverride each other because the receiver is different(AlarmReceiverFirst and AlarmReceiverSecond)

从上面的例子中,它们不会因为接收器不同而相互覆盖(AlarmReceiverFirst 和 AlarmReceiverSecond)

Intent startIntent2 = new Intent(context, AlarmReceiverSecond.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, 0);

Intent startIntent3 = new Intent(context, AlarmReceiverSecond.class);
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(context, 0, startIntent3, 0);

From above example, they willoverride each other, because the receiver is same(AlarmReceiverSecond)

从上面的例子中,它们相互覆盖,因为接收器是相同的(AlarmReceiverSecond)

回答by JSONParser

in my case i want to open the same activity with two different intents so if two or more FCMS are there in the tray, any one of them will only open other will not, so i changed the requests codes of pending intent then it worked.

在我的情况下,我想用两个不同的意图打开同一个活动,所以如果托盘中有两个或多个 FCMS,它们中的任何一个只会打开另一个不会,所以我更改了待处理意图的请求代码然后它起作用了。

 PendingIntent pendingIntent =
                            PendingIntent.getActivity(this, **Some unique id for all GCMS** /* Request code */, intent,
                                    PendingIntent.FLAG_ONE_SHOT);

回答by Alireza Jamali

one important thing about requestCodethat will seriously trouble your app is when using widgets. widgets will not work after phone reboot if their requestCodeare the same. that means the pendingIndentyou set on the remoteViewsof your widget must be set unique requestCode, usually the widgetId accompanying a number.

requestCode使用小部件时会严重困扰您的应用程序的一件重要事情是。如果小部件requestCode相同,则手机重启后小部件将无法工作。这意味着pendingIndent您在remoteViews小部件上设置的必须设置唯一的 requestCode,通常是伴随数字的 widgetId。

回答by Eir

Actually, the documentationclearly states what the request code is used for:

实际上,文档清楚地说明了请求代码的用途:

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 to getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

如果您确实需要同时激活多个不同的 PendingIntent 对象(例如用作同时显示的两个通知),那么您需要确保它们有一些不同之处,以便将它们与不同的待定意图。这可能是 Intent#filterEquals(Intent) 考虑的任何 Intent 属性,或提供给 getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast( Context, int, Intent, int) 或 getService(Context, int, Intent, int)。

Since it seems that it still isn't that clear, let me try to explain:

好像还不是很清楚,我试着解释一下:

When you want to use a PendingIntentobject, you don't just instantiate one. Rather, you obtain one from the system using the PendingIntentstatic methods (getActivity, getBroadcast, getServiceetc). The system keeps a bunch of PendingIntent instances and gives you one. Which one it gives you, it depends on the input parameters you pass to these getter methods. Those input parameters are: Context, i.e. the target receiver of the intent, the Intentto use, requestCodeand flags. When you pass the same Context, the same requestCodeand the same Intent (meaning an intent that filterEqualswith another intent), you get the same PendingIntentobject. The point is that the system wants to have as few PendingIntentobjects as possible, so it tends to reuse the existing ones, as much as possible.

当你想使用一个PendingIntent对象时,你不只是实例化一个。相反,你获得一个使用该系统的PendingIntent静态方法(getActivitygetBroadcastgetService等)。系统保留一堆 PendingIntent 实例并给你一个。它给你哪一个,这取决于你传递给这些 getter 方法的输入参数。这些输入参数是:Context,即意图的目标接收者、Intent使用的对象requestCodeflags。当您传递相同Context、相同requestCode和相同的 Intent(意味着一个意图filterEquals与另一个意图)时,您将获得相同的PendingIntent对象。关键是系统希望拥有尽可能少的PendingIntent对象,因此它倾向于尽可能多地重用现有的对象。

For example, you have two calendar notifications, for two different dates. When you click on one of them, you want your app to open to the corresponding date of that notification. In that scenario, you have the same Contexttarget, and the Intentobject you are passing differ only in the EXTRA_DATA (which specifies the date that should be open). If you provide the same requestCodewhen obtaining the PendingIntentobject, then you will end up with the same PendingIntentobject. So, when creating the second notification, you will replace the old Intentobject with the new EXTRA_DATA, and end up with two notifications pointing to the same date.

例如,您有两个日历通知,分别对应两个不同的日期。当您单击其中一个时,您希望您的应用程序打开到该通知的相应日期。在这种情况下,您具有相同的Context目标,并且Intent您传递的对象仅在 EXTRA_DATA(指定应该打开的日期)中有所不同。如果您requestCode在获取PendingIntent对象时提供相同的内容,那么您最终将获得相同的PendingIntent对象。因此,在创建第二个通知时,您将用Intent新的 EXTRA_DATA替换旧对象,并最终得到两个指向同一日期的通知。

If you want to have two different PendingIntentobjects, as you should in this scenario, you should specify a different requestCodewhen obtaining the PendingIntentobject.

如果你想拥有两个不同的PendingIntent对象,就像你在这个场景中应该的那样,你应该requestCode在获取PendingIntent对象时指定一个不同的对象。