【发布时间】:2010-06-29 11:12:53
【问题描述】:
几天前,我一直在努力寻找一种方法来为我的闹钟使用自定义意图。虽然我得到了明确的答案,但我必须根据一些唯一的 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);
这样工作:
- 应用程序为联系人创建消息
- 提供了一个意图,其中包含联系人 ID 和有关消息的详细信息
- 随消息一起引发通知
- 用户对通知的操作和应用程序显示意图传递的消息
问题
对于一个联系人,这种情况可能会发生不止一次。当生成第二条消息时,通知会很好地引发(那里的消息很好),但用户操作通知时的意图是使用旧数据,因此传递的是之前的消息而不是全新的消息。
因此,在某种程度上,意图是缓存和重用以前的附加内容。如何使每个联系人和每个操作都独一无二?
【问题讨论】:
-
有没有办法清除所有缓存的 IntentExtras?我想我现在修复了它,但旧的缓存意图仍然存在......
-
根据 Intent 的标志或活动的启动模式,可能会出现类似的问题。在这种情况下,您需要检查Activity::onNewIntent,因为 Activity::getIntent 将返回 Activity 的 ORIGINAL Intent,而不是更新后的 action/extras/etc 的新 Intent。
标签: android android-intent extras