Android的不断缓存我的意图附加功能,如何声明挂起的意图,保持新鲜的演员? [英] Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?

查看:91
本文介绍了Android的不断缓存我的意图附加功能,如何声明挂起的意图,保持新鲜的演员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天我在努力寻找一种方法,使用自定义的意图我报警。虽然我得到了明确的答案,我要的意图基于一些独特的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);

这是这样的:

  • 应用程序联系人
  • 创建消息
  • 意图设置有接触ID和细节有关消息
  • 在通知中提出的信息
  • 在用户actiones的通知和应用程序显示的意图传递的信息

问题

这可能发生一次以上的联系人。并产生第二消息时,该通知被升高以充分(消息是细存在),但是,当用户的操作,它使用旧的数据的通知,所以previous消息传递的意图,而不是全新的信息。

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.

所以好歹的意图是缓存和重用previous演员。怎样才能让每个联系人,每个动作是独一无二的?

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

推荐答案

如果只有一个你的 PendingIntents 此联系人将未完成的时候,或者任何时候你总是希望使用最新的一组额外的,使用 FLAG_UPDATE_CURRENT 在创建 PendingIntent

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 显然可以用于创建不同的 PendingIntent 对象为同一标的意图,虽然我从来没有尝试过这一点。

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的不断缓存我的意图附加功能,如何声明挂起的意图,保持新鲜的演员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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