首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Android:管理多个通知

Android:管理多个通知
EN

Stack Overflow用户
提问于 2010-08-12 14:31:52
回答 2查看 8.9K关注 0票数 9

我正在尝试在我的应用程序中创建多个通知。为了唯一地标识每个通知,我为它们提供了一个唯一的identificationId。以下是我的代码:

代码语言:javascript
运行
复制
private void updateNotification(int notificationId, int clockStatusID, CharSequence text) {
 //notificationManager.cancel(notificationId);
// throws up an ongoing notification that the timer is running
Log.i("TIMERCOUNT", "Notification id: " + notificationId);
Notification not = new Notification(clockStatusID, // the
    // icon
    // for
    // the
    // status
    // bar
    text, // the text to display in the ticker
    System.currentTimeMillis() // the timestamp for the
    // notification to appear
);
Intent intent = new Intent();
intent.putExtra("notificationID", notificationId);
intent.setAction("actionstring" + System.currentTimeMillis());
intent.setClassName("com.chander.time.android.activities",
"com.chander.time.android.activities.Tabs");


not.setLatestEventInfo(self,
    getText(R.string.timer_notification_title),
    getText(R.string.timer_on_notification_text), PendingIntent
    .getActivity(this, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT));

not.flags += Notification.FLAG_ONGOING_EVENT;
not.flags += Notification.FLAG_NO_CLEAR;
notificationManager.notify(notificationId, not);
}

问题:当选择一个通知时,Tabs活动被调用来传递意图。我想要访问在选项卡中选择的通知的唯一notificationId。我尝试使用intent.putExtra()将notificationId保存在intent中。但是,对于多个通知,它会覆盖notificationId并返回最新的通知。我不明白为什么会发生这种情况,我怎样才能避免这种对notificationId的覆盖。

谢谢,钱德

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-13 01:39:51

我知道答案了。意图被缓存了进去。要创建新的意图,只需添加以下代码:

代码语言:javascript
运行
复制
saveCallIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));

这使得意图独一无二。

此外,有人向我推荐了以下代码,以使意图具有唯一性:

代码语言:javascript
运行
复制
saveCallIntent.setAction("actionstring" + System.currentTimeMillis());

它对我没有帮助,但可能对别人有帮助。

--钱德

票数 16
EN

Stack Overflow用户

发布于 2012-02-24 15:44:42

只需在pendingIntent中放置notificationId而不是0。

代码语言:javascript
运行
复制
PendingIntent.getActivity(this, notificationId, intent,PendingIntent.FLAG_UPDATE_CURRENT));

用户还必须同时更新以下代码和上面的代码,才能使其正常工作。

代码语言:javascript
运行
复制
 mNotificationManager.notify(notificationId, mBuilder.build());
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3465230

复制
相关文章

相似问题

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