是否有可能创建多个PendingIntents具有相同的请求code和不同的演员? [英] Is it possible to create multiple PendingIntents with the same requestCode and different extras?

查看:104
本文介绍了是否有可能创建多个PendingIntents具有相同的请求code和不同的演员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 AlarmManager 来调度之间的任何位置1和35的报警(根据用户输入)。当用户请求安排新的报警,我要取消当前的报警,所以我创建了我所有的报警具有相同的请求code,中定义的最后变量。

I'm using AlarmManager to schedule anywhere between 1 and 35 alarms (depending on user input). When the user requests to schedule new alarms, I need to cancel the current alarms, so I create all of my alarms with the same requestCode, defined in a final variable.

// clear remaining alarms
Intent intentstop = new Intent(this, NDService.class);
PendingIntent senderstop = PendingIntent.getService(this,
            NODIR_REQUESTCODE, intentstop, 0);
am.cancel(senderstop);

// loop through days
if (sched_slider.getBooleanValue())
for (int day = 1; day < 8; day++) {

    if (day == 1 && sun.isChecked())
                scheduleDay(day);
    if (day == 2 && mon.isChecked())
                scheduleDay(day);
    if (day == 3 && tue.isChecked())
                scheduleDay(day);
    if (day == 4 && wed.isChecked())
                scheduleDay(day);
    if (day == 5 && thu.isChecked())
                scheduleDay(day);
    if (day == 6 && fri.isChecked())
                scheduleDay(day);
    if (day == 7 && sat.isChecked())
                scheduleDay(day);
}

...

public void scheduleDay(int dayofweek) {
    Intent toolintent = new Intent(this, NDService.class);
    toolintent.putExtra("TOOL", "this value changes occasionally");
    PendingIntent pi = PendingIntent.getService(this,
                NODIR_REQUESTCODE, toolintent, 0);
    calendar.set(Calendar.DAY_OF_WEEK, dayofweek);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);
    am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY * 7, pi);
}

下面,如果用户有阳光(这是一个复选框)选中,将安排报警,在运行每个星期天一小时。你可以看到,这种方式创建每个报警都具有相同的请求code,但工具额外有时变化为每个报警。

Here, if the user has sun (which is a CheckBox) checked, it will schedule an alarm to run every Sunday at hour and minute. You can see that every alarm created this way has the same requestCode, but the TOOL extra changes sometimes for each alarm.

不过,在我的测试中,当警报响起时,我的服务运行,从意向的演员,现在。 <一href="http://stackoverflow.com/questions/2882459/getextra-from-intent-launched-from-a-pendingintent">This问题表明,使用 PendingIntent.FLAG_CANCEL_CURRENT 将解决这个问题,但不会说取消其他PendingIntents?

However, in my testing, when the alarm goes off and my Service runs, the extras from the Intent are now null. This question suggests that using PendingIntent.FLAG_CANCEL_CURRENT will solve this, but wouldn't that cancel the other PendingIntents?

有人能解释如何PendingIntents工作,对与相同的请求code和不同的演员创建​​多个的呢?我应该是什么标志(如果有的话)使用?

Can someone explain how PendingIntents work, in reference to creating multiple ones with the same requestCode and different extras? What flags (if any) should I be using?

推荐答案

其实,你没有创造 PendingIntent 秒。您可以从Android框架提出要求。当您请求 PendingIntent 从Android框架,它会检查,看看是否已经有一个 PendingIntent 符合条件你作为参数传递。如果是这样,它不会创建一个新的 PendingIntent ,它只是给你回一个标记,指向现有的 PendingIntent 。如果没有找到匹配的 PendingIntent ,它会创建一个,然后给你回一个令牌指向它刚刚创建的。有一些标志,你可以设置修改此行为,但并不多。在这里了解,最重要的是Android架构确实匹配的方式。

Actually, you don't "create" PendingIntents. You request them from the Android framework. When you request a PendingIntent from the Android framework, it checks to see if there is already a PendingIntent that matches the criteria you pass as arguments. If so, it does not create a new PendingIntent, it just gives you back a "token" that points to the existing PendingIntent. If it doesn't find a matching PendingIntent, it will create one and then give you back a "token" that points to the one it just created. There are some flags that you can set to modify this behaviour, but not that much. The most important thing to understand here is the way the Android framework does the matching.

要做到这一点检查,如果下面的参数匹配(比较现有的 PendingIntent 与您已通过参数):

To do this it checks if the following parameters match (comparing the existing PendingIntent with the parameters you have passed):

  • 请求codeS必须是相同的。否则,他们不匹配。
  • 动作意图必须是相同(或两个零)。否则,他们不匹配。
  • 数据意向必须是相同(或两个零)。否则,他们不匹配。
  • 在类型(数据)在意图必须是相同(或两个零)。否则,他们不匹配。
  • 在包和/或意图组件必须是相同(或两个零)。否则它们不匹配。 包和组件字段为明确的意图 S设定。
  • 类别列表意向必须是相同的。否则,他们不匹配。
  • Request codes must be the same. Otherwise they do not match.
  • The "action" in the Intent must be the same (or both null). Otherwise they do not match.
  • The "data" in the Intent must be the same (or both null). Otherwise they do not match.
  • The "type" (of the data) in the Intent must be the same (or both null). Otherwise they do not match.
  • The "package" and/or "component" in the Intent must be the same (or both null). Otherwise they do not match. "package" and "component" fields are set for "explicit" Intents.
  • The list of "categories" in the Intent must be the same. Otherwise they do not match.

您应该注意到,额外的不在上面列表中。这意味着,如果你请求一个 PendingIntent 的额外不考虑在Android框架试图找到匹配的 PendingIntent 。这是一个常见的​​错误,使开发人员

You should notice that "extras" is not in the list above. That means that if you request a PendingIntent the "extras" are not taken into consideration when the Android framework tries to find a matching PendingIntent. This is a common mistake that developers make.

我们现在可以解决其他标志,您可以添加修改 PendingIntent 请求的行为:

We can now address the additional flags that you can add to modify the behaviour of a PendingIntent request:

FLAG_CANCEL_CURRENT - 当你指定这个标志,如果匹配的是 PendingIntent 的发现,即 PendingIntent 取消(删除,删除无效),并创建一个新的。这意味着任何应用程序都拿着令牌指向旧<$​​ C $ C> PendingIntent 将无法使用它,因为它不再有效。

FLAG_CANCEL_CURRENT - When you specify this flag, if a matching PendingIntent is found, that PendingIntent is cancelled (removed, deleted, invalidated) and a new one is created. This means that any applications that are holding a "token" pointing to the old PendingIntent will not be able to use it, because it is no longer valid.

FLAG_NO_CREATE - 当你指定这个标志,如果匹配的是 PendingIntent 被发现,一个象征性指向现有的 PendingIntent 返回(这是通常的行为)。但是,如果没有匹配的 PendingIntent 被发现,一个新的未创建并调用只是返回。这可以被用来确定是否有活性 PendingIntent 为一组特定的参数

FLAG_NO_CREATE - When you specify this flag, if a matching PendingIntent is found, a "token" pointing to the existing PendingIntent is returned (this is the usual behaviour). However, if no matching PendingIntent is found, a new one is not created and the call just returns null. This can be used to determine if there is an active PendingIntent for a specific set of parameters.

FLAG_ONE_SHOT - 当你指定这个标志, PendingIntent 创建只能使用一次。这意味着,如果你给的令牌这个 PendingIntent 多个应用程序,在第一次使用后的 PendingIntent 它将被取消(删除,删除,失效),使得任何将来试图使用它将会失败。

FLAG_ONE_SHOT - When you specify this flag, the PendingIntent that is created can only be used once. That means that if you give the "token" for this PendingIntent to multiple applications, after the first use of the PendingIntent it will be canceled (removed, deleted, invalidated) so that any future attempt to use it will fail.

FLAG_UPDATE_CURRENT - 当你指定这个标志,如果匹配的是 PendingIntent 被找到,群众​​演员,在 PendingIntent 将在意图,你作为参数传递给<$ C $的群众演员取代C>的getXXX()方法。如果没有匹配的 PendingIntent 被发现,一个新创建(这是正常的行为)。这可以用来改变的额外的现有 PendingIntent ,你已经给了象征性到其他应用程序,并且不希望无效现有的 PendingIntent

FLAG_UPDATE_CURRENT - When you specify this flag, if a matching PendingIntent is found, the "extras" in that PendingIntent will be replaced by the "extras" in the Intent that you pass as a parameter to the getxxx() method. If no matching PendingIntent is found, a new one is created (this is the normal behaviour). This can be used to change the "extras" on an existing PendingIntent where you have already given the "token" to other applications and don't want to invalidate the existing PendingIntent.

让我尝试解决您的特定问题:

Let me try to address your specific problem:

您不能有多个活动 PendingIntent 中,如果请求code,动作,数据类型和封装/组件参数都是相同的系统。所以,你的要求,可以有多达35活性 PendingIntent 一切都具有相同的请求code,动作,数据类型和封装/,但组件参数不同的临时演员,是不可能的。

You cannot have more than one active PendingIntent in the system if the request code, action, data, type and package/component parameters are the same. So your requirement to be able to have up to 35 active PendingIntents all with the same request code, action, data, type and package/component parameters, but with different "extras", is not possible.

我会建议你要么使用35个不同的请求codeS,或者创建35个不同的独特的动作参数的意图

I would suggest that you either use 35 different request codes, or create 35 different unique "action" parameters for your Intent.

这篇关于是否有可能创建多个PendingIntents具有相同的请求code和不同的演员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆