gpt4 book ai didi

android - 是否可以使用相同的 requestCode 和不同的附加项创建多个 PendingIntent?

转载 作者:IT老高 更新时间:2023-10-28 23:16:50 28 4
gpt4 key购买 nike

我正在使用 AlarmManager安排 1 到 35 个警报之间的任意位置(取决于用户输入)。当用户请求安排新的闹钟时,我需要取消当前的闹钟,因此我使用相同的 requestCode 创建所有闹钟,在 final 中定义。多变的。

// 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);
}

这里,如果用户有 sun (这是一个复选框)选中,它会安排一个警报在每周日在 hour 运行。和 minute .您可以看到以这种方式创建的每个警报都有相同的 requestCode,但是 TOOL有时每个警报都会有额外的变化。

然而,在我的测试中,当警报响起并且我的服务运行时,来自 Intent 的额外信息现在是 null . This question建议使用 PendingIntent.FLAG_CANCEL_CURRENT会解决这个问题,但不会取消其他 PendingIntents 吗?

简而言之:

有人可以解释 PendingIntents 是如何工作的,引用使用相同的 requestCode 和不同的附加功能创建多个?我应该使用哪些标志(如果有)?

最佳答案

其实,你不是“创造”PendingIntent s。您从 Android 框架请求它们。当您请求 PendingIntent 时在 Android 框架中,它会检查是否已经存在 PendingIntent与您作为参数传递的条件相匹配。如果是这样,它不会创建新的 PendingIntent ,它只是给你一个指向现有 PendingIntent 的“ token ” .如果没有找到匹配的 PendingIntent ,它会创建一个,然后给你一个“ token ”,指向它刚刚创建的那个。您可以设置一些标志来修改此行为,但不是那么多。这里要理解的最重要的事情是 Android 框架进行匹配的方式。

为此,它会检查以下参数是否匹配(将现有的 PendingIntent 与您传递的参数进行比较):

  • 请求代码必须相同。否则它们不匹配。
  • Intent中的“ Action ”必须相同(或都为空)。否则它们不匹配。
  • Intent中的“数据”必须相同(或都为空)。否则它们不匹配。
  • Intent 中的(数据的)“类型”必须相同(或都为空)。否则它们不匹配。
  • Intent 中的“包”和/或“组件”必须相同(或都为空)。否则它们不匹配。 “包”和“组件”字段设置为“显式”Intent s。
  • Intent中的“类别”列表必须相同。否则它们不匹配。

  • 您应该注意到“额外” 不在以上列表中 .这意味着如果您请求 PendingIntent当 Android 框架试图找到匹配的 PendingIntent 时,不会考虑“额外”。 .这是开发人员常犯的错误。

    我们现在可以解决您可以添加的附加标志来修改 PendingIntent 的行为。要求:
    FLAG_CANCEL_CURRENT - 当您指定此标志时,如果匹配 PendingIntent发现, PendingIntent被取消(移除、删除、失效)并创建一个新的。这意味着任何持有指向旧 PendingIntent 的“ token ”的应用程序。将无法使用它,因为它不再有效。
    FLAG_NO_CREATE - 当您指定此标志时,如果匹配 PendingIntent找到,指向现有 PendingIntent 的“ token ”返回(这是通常的行为)。但是,如果没有匹配 PendingIntent发现,一个新的 未创建 并且调用只返回 null .这可用于确定是否存在 Activity PendingIntent用于一组特定的参数。
    FLAG_ONE_SHOT - 当您指定此标志时, PendingIntent创建的只能使用一次。这意味着如果您为此 PendingIntent 提供“ token ”到多个应用程序,第一次使用后 PendingIntent它将被取消(删除、删除、失效),以便将来使用它的任何尝试都将失败。
    FLAG_UPDATE_CURRENT - 当您指定此标志时,如果匹配 PendingIntent找到了,其中的“额外内容” PendingIntent将被 Intent 中的“额外”替换您作为参数传递给 getxxx()方法。如果不匹配 PendingIntent找到,创建一个新的(这是正常行为)。这可用于更改现有 PendingIntent 上的“附加功能”您已经将“ token ”提供给其他应用程序并且不想使现有的 PendingIntent 失效的地方.

    让我尝试解决您的具体问题:

    您不能有多个 Activity PendingIntent如果请求代码、 Action 、数据、类型和包/组件参数相同,则在系统中。因此,您要求最多可以拥有 35 个 Activity PendingIntent s 都具有相同的请求代码、 Action 、数据、类型和包/组件参数,但具有不同的“附加”,这是不可能的。

    我建议您要么使用 35 个不同的请求代码,要么为您的 Intent 创建 35 个不同的唯一“操作”参数。 .

    关于android - 是否可以使用相同的 requestCode 和不同的附加项创建多个 PendingIntent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20204284/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com