首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在创建安卓通知时,在PendingIntent中使用FLAG_UPDATE_CURRENT时,intent extras会重复

在创建安卓通知时,在PendingIntent中使用FLAG_UPDATE_CURRENT时,intent extras会重复
EN

Stack Overflow用户
提问于 2012-05-10 23:11:02
回答 1查看 8.1K关注 0票数 19

我想创建多个通知,用于启动(或刷新)活动以显示产品描述。

代码语言:javascript
复制
Notification notification = new Notification(R.drawable.applicationicon,
            Resources.getString("NewSaleNotification", context),
            System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(context, MainApplication.class);
intent.putExtra("saleid", saleid);

// to be sure the activity won't be restarted
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, SaleTitle, SaleMessage, pendingIntent);
notificationManager.notify(saleid, notification);

当我创建PendingIntent时,我有4个选择: FLAG_CANCEL_CURRENT、FLAG_NO_CREATE、FLAG_ONE_SHOT和FLAG_UPDATE_CURRENT。

最后一个(http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT)的定义是我想要做的事情,但它不能正常工作。如果我创建两个通知,它们都有相同的'saleid‘额外的,这是最新的。如何使用不同的'saleid‘额外发出多个通知?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-11 00:37:26

,但它不能正常工作

是的,确实如此。

如果我创建两个通知,它们都有相同的'saleid‘额外的,这是最新的一个。

这正是the documentation所说的应该发生的事情。

如何使用不同的'saleid‘额外发出多个通知?

选项#1:在每个Intents中放置不同的操作字符串。这将使它们不同(从filterEquals()的角度来看),并为它们提供独立的PendingIntents。但是,由于您是在Intent (MainApplication.class)中指定组件,因此该操作不会影响Intent的路由方式。

选项#2:在getActivity()调用中使用不同的requestCode (第二个参数)。虽然这被记录为“当前未使用”,但它确实会导致返回不同的PendingIntent对象。但是,由于此行为未记录在案,因此将来可能会发生变化。

票数 28
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10537006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档