[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?

摘要:
但是,你会发觉无论点哪个Notification,传递回来的都是最后被notify的Intent。找了很久,试了改变PendingIntent的flag也无果,最后还是在这帖子里找到答案,我来总结下:问题主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数,实际上是通过该参数来区别不同的Intent的,如果id相同,就会覆盖掉之前的Intent了。所以总是获取到最后一个Intent。

情况是这样的,使用NotificationManager触发多个Notification:

Java代码 复制代码收藏代码[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?第3张
  1. privateNotificationgenreNotification(Contextcontext,inticon,StringtickerText,Stringtitle,Stringcontent,Intentintent){
  2. Notificationnotification=newNotification(icon,tickerText,System.currentTimeMillis());
  3. PendingIntentpendIntent=PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
  4. notification.setLatestEventInfo(context,title,content,pendIntent);
  5. notification.flags|=Notification.FLAG_AUTO_CANCEL;
  6. returnnotification;
  7. }
  8. ...
  9. mNotificationManager.notify(ID_1,
  10. genreNotification(mContext,ICON_RES,
  11. notifyText1,notifyTitle1,notifyText1,intent_1));
  12. ...
  13. mNotificationManager.notify(ID_2,
  14. genreNotification(mContext,ICON_RES,
  15. notifyText2,notifyTitle2,notifyText2,intent_2));
  16. ...
  17. mNotificationManager.notify(ID_3,
  18. genreNotification(mContext,ICON_RES,
  19. notifyText3,notifyTitle3,notifyText3,intent_3));
private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent){
        Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
        PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, title, content, pendIntent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        return notification;
    }
...
mNotificationManager.notify(ID_1, 
                    genreNotification(mContext, ICON_RES, 
                            notifyText1, notifyTitle1, notifyText1, intent_1));
...
mNotificationManager.notify(ID_2, 
                    genreNotification(mContext, ICON_RES, 
                            notifyText2, notifyTitle2, notifyText2, intent_2));
...
mNotificationManager.notify(ID_3, 
                    genreNotification(mContext, ICON_RES, 
                            notifyText3, notifyTitle3, notifyText3, intent_3));

可见ID和Intent都是不同的,生成的PendingIntent分别对应着不同的Intent。但是,你会发觉无论点哪个Notification,传递回来的都是最后被notify的Intent。这里即intent_3。

找了很久,试了改变PendingIntent的flag也无果,最后还是在这帖子里找到答案(CSDN帖子 ),我来总结下:

问题主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同,就会覆盖掉之前的Intent了。所以总是获取到最后一个Intent。

只要每个不同的Intent对应传递一个独立的ID就可以了,以上函数修改如下(增加ID参数):

Java代码 复制代码收藏代码[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?第3张
  1. privateNotificationgenreNotification(Contextcontext,inticon,StringtickerText,Stringtitle,Stringcontent,Intentintent,intid){
  2. Notificationnotification=newNotification(icon,tickerText,System.currentTimeMillis());
  3. //问题就在这里的id了
  4. PendingIntentpendIntent=PendingIntent.getActivity(context,id,intent,PendingIntent.FLAG_UPDATE_CURRENT);
  5. notification.setLatestEventInfo(context,title,content,pendIntent);
  6. notification.flags|=Notification.FLAG_AUTO_CANCEL;
  7. returnnotification;
  8. }
  9. ...
  10. mNotificationManager.notify(ID_1,
  11. genreNotification(mContext,ICON_RES,
  12. notifyText1,notifyTitle1,notifyText1,intent_1,ID_1));
  13. ...
  14. mNotificationManager.notify(ID_2,
  15. genreNotification(mContext,ICON_RES,
  16. notifyText2,notifyTitle2,notifyText2,intent_2,ID_2));
  17. ...
  18. mNotificationManager.notify(ID_3,
  19. genreNotification(mContext,ICON_RES,
  20. notifyText3,notifyTitle3,notifyText3,intent_3,ID_3));

免责声明:文章转载自《[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MQTT协议中的topic8.3 Android灯光系统_编写HAL_lights.c下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ANR的原理分析和简单总结

前言ANR(Application Not Responding),应用无响应,这个可能每个人都碰到过。 该篇主要简单总结下,ANR的几种常见类型(输入事件、广播接收器、Service、ContentProvider),以及ANR一般如何产生的及如何避免。 最后重点是通过源码 了解这几种类型 是如何产生ANR、超时时间是怎么来的、ANR后如何处理的等。 关...

转:Android进阶:模拟闹钟 学习Alarm与Notification

无意间看到Alarm这个类 觉得挺有意思 这个用法应该会比较常用到 看了一些介绍 然后自己写了一个demo Alarm是在预定的时间上触发Intent的一种独立的方法。Alarm超出了应用程序的作用域,所以它们可以用于触发应用程序事件或动作,甚至在应用程序关闭之后,与Broadcast Receiver结合,它们可以变得尤其的强大,可以通过设置Alarm来...

AndroidManifest.xml配置文件详解 (转)

AndroidManifest.xml配置文件对于Android应用开发来说是非常重要的基础知识,本文旨在总结该配置文件中重点的用法,以便日后查阅。下面是一个标准的AndroidManifest.xml文件样例。     [html] view plaincopy   <?xml version="1.0" encoding="utf-8"?>...

android 之 桌面的小控件AppWidget

AppWidget是创建的桌面窗口小控件,在这个小控件上允许我们进行一些操作(这个视自己的需要而定)。作为菜鸟,我在这里将介绍一下AppWeight的简单使用。 1.在介绍AppWidget之前,我们先来了解一下PendingIntent和RemoteViews; PendingIntent:A description of an Intent and t...

状态通知栏

在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等。 下面就来说说经常会使用到通知机制中的通知栏框架(Notificaiton),它适用于交互事件的通知。它是位于顶层可以展开的通知列表。它会时不时的提醒你什么软件...

Android开发之AIDL的使用一--跨应用启动Service

启动其他App的服务,跨进程启动服务。 与启动本应用的Service一样,使用startService(intent)方法 不同的是intent需要携带的内容不同,需要使用intent的setComponent()方法。 setComponent()方法需要传入两个参数,第一个参数是包名,第二个参数是组件名。即,第一个参数传入要启动的其他app的包名,第二...