Android 不断缓存我的 Intents Extras,如何声明一个挂起的 Intent 以保持新鲜的 extras? [英] Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?

查看:19
本文介绍了Android 不断缓存我的 Intents Extras,如何声明一个挂起的 Intent 以保持新鲜的 extras?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A few days ago I was struggling to find a way to use custom intents for my alarms. Although I got clear answer that I have to customize the Intents based on some unique ID eg. setAction() still have some problems.

我以这种方式定义了一个 PendingIntent:

I define a PendingIntent this way:

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

然后这是由通知管理器使用

then this is used by a notification manager

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 和有关消息的详细信息
  • 通过消息发出通知
  • 用户对通知进行操作,应用显示意图传递的消息

问题

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

This can happen more than once for a contact. And when the second message is generated, the notification is raised well (message is fine there) but the intent when the user actions the notification it uses old data, so previous message is passed and not the brand new message.

所以某种程度上的意图是缓存和重用以前的额外内容.如何使每个联系人和每个操作都独一无二?

So someway the intent is caching and reusing previous extras. How can I make it unique per contact and per action?

推荐答案

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

If only one of your PendingIntents for this contact will be outstanding at any point in time, or if you always want to use the latest set of extras, use FLAG_UPDATE_CURRENT when you create the PendingIntent.

如果多个特定于联系人的 PendingIntent 将同时突出,并且它们需要有单独的附加项,则您需要添加计数或时间戳或其他内容来区分它们.

If more than one contact-specific PendingIntent will be outstanding at once, and they need to have separate extras, you will need to add a count or timestamp or something to distinguish them.

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

<小时>

更新

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

Also, the lightly-documented second parameter to getActivity() and kin on PendingIntent apparently can be used to create distinct PendingIntent objects for the same underlying Intent, though I have never tried this.

这篇关于Android 不断缓存我的 Intents Extras,如何声明一个挂起的 Intent 以保持新鲜的 extras?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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