Android 不断缓存我的意图 Extras,如何声明保留新鲜附加值的挂起意图?

发布于 2024-09-07 15:05:05 字数 1298 浏览 2 评论 0原文

几天前,我正在努力寻找一种为我的警报使用自定义意图的方法。尽管我得到了明确的答案,我必须根据一些唯一的 ID 自定义意图,例如。 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 提供意图,并
  • 引发有关消息通知的
  • 通过消息用户对消息的操作 详细信息。通知,应用程序显示意图传递的消息

问题

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

所以无论如何,目的是缓存和重用以前的额外内容。如何使每个联系人和每个操作都独一无二?

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.

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

This works like this:

  • application creates a message for a contact
  • an intent is provided with the contact id and details about the message
  • notification is raised with the message
  • user actiones on the notification and the app displays the message passed by the intent

The problem

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-09-14 15:05:05

如果此联系人在任何时间点只有一个 PendingIntents 未完成,或者如果您始终想使用最新的一组额外内容,请在创建时使用 FLAG_UPDATE_CURRENT PendingIntent

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

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

更新

此外,getActivity() 的第二个参数和 PendingIntent 上的 kin 显然可以用于创建不同的 PendingIntent< /code> 对象具有相同的底层 Intent,尽管我从未尝试过这一点。

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.

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

UPDATE

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.

情话难免假 2024-09-14 15:05:05

我通常指定唯一的 requestCode 以防止我的 PendingIntents 相互覆盖:

PendingIntent pending = PendingIntent.getService(context, unique_id, intent, 0);

在您的情况下,我同意 CommonsWare 您只需要 FLAG_UPDATE_CURRENT 标志。新的附加值将覆盖旧值。

I usually specify unique requestCode to prevent my PendingIntents from overriding each other:

PendingIntent pending = PendingIntent.getService(context, unique_id, intent, 0);

And in your case I agree with CommonsWare you just need FLAG_UPDATE_CURRENT flag. New extras will override old values.

前事休说 2024-09-14 15:05:05

找到了一个简单的解决方案,技巧是请求代码。

int requestCode = (int) System.currentTimeMillis();

PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE);

如果您对每个 PendingIntent 使用相同的请求代码,Android 会将其视为相同的请求,并将返回您在第一个请求中放入的相同额外内容。

Found a simple solution, trick is the request code.

int requestCode = (int) System.currentTimeMillis();

PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE);

If you will use same request code for each PendingIntent Android will consider it as same request which will return same Extras which you put in your first request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文