Android 不断缓存我的 Intents Extras,如何声明一个未决的 Intent 来保留新的 Extras?

标签 android android-intent extras

几天前,我一直在努力寻找一种方法来为我的闹钟使用自定义 Intent 。虽然我得到了明确的答案,但我必须根据一些唯一的 ID 来定制 Intent,例如。 setAction()还是有一些问题。

我这样定义一个 PendingIntent:

Intent intent = new Intent(this, viewContactQuick.class);
intent.setAction("newmessage"+objContact.getId());//unique per contact
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK ).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP );
intent.putExtra("id", Long.parseLong(objContact.getId()));
intent.putExtra("results", result.toArray());

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);

然后这被通知管理器使用

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
// first try to clear any active notification with this contact ID
mNotificationManager.cancel(Integer.parseInt(objContact.getId()));

// then raise a new notification for this contact ID
mNotificationManager.notify(Integer.parseInt(objContact.getId()), notification);

这样工作:

  • 应用程序为联系人创建消息
  • 提供了一个 Intent ,其中包含联系人 ID 和有关消息的详细信息
  • 随消息发出通知
  • 用户对通知的操作和应用显示 Intent 传递的消息

问题

对于一个联系人,这种情况可能会发生不止一次。当生成第二条消息时,通知会很好地引发(那里的消息很好),但用户操作通知时的 Intent 是使用旧数据,因此传递的是之前的消息而不是全新的消息。

因此,在某种程度上, Intent 是缓存和重用以前的附加内容。如何使每个联系人和每个操作都独一无二?

最佳答案

如果您的此联系人的 PendingIntents 中只有一个在任何时间点未完成,或者如果您总是想使用最新的一组附加内容,请使用 FLAG_UPDATE_CURRENT当你创建 PendingIntent.

如果不止一个特定于联系人的 PendingIntent 将同时处于未完成状态,并且它们需要有单独的附加项,则您需要添加计数或时间戳或其他东西来区分它们。

intent.setAction("actionstring" + System.currentTimeMillis());

更新

此外,getActivity() 的第二个参数和 PendingIntent 上的 kin 显然可用于创建不同的 PendingIntent 对象相同的底层 Intent,虽然我从未尝试过。

关于Android 不断缓存我的 Intents Extras,如何声明一个未决的 Intent 来保留新的 Extras?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3140072/

相关文章:

android - 检测呼出电话是否已被接听

java - 将提供者作为 Intent 的额外内容传递

android - Activity 没有得到正确的包

Java提取字符串列表中的每个HTML内容

android - 没有 Gradle - 找不到与给定名称匹配的资源(在 'value' 处,值为 '@integer/google_play_services_version' )

android - codename one android拦截urls问题

Android 将多个号码传递给 SMS Intent

android - 如何通过 Intent 将数据从小部件配置类传递到小部件提供程序类?

Android 状态栏要求图标大小为 25x25dp,而指南建议大小为 32x32dp。谁错了?

Android : EditText. getText() 在 Android 1.6 上返回 ""(空)字符串