[问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
来源:互联网 发布:qq mac版怎么更新 编辑:程序博客网 时间:2024/04/29 10:01
情况是这样的,使用NotificationManager触发多个Notification:
- 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));
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参数):
- private Notification genreNotification(Context context, int icon, String tickerText, String title, String content, Intent intent, int id){
- Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
- // 问题就在这里的id了
- PendingIntent pendIntent = PendingIntent.getActivity(context, id, 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, ID_1));
- ...
- mNotificationManager.notify(ID_2,
- genreNotification(mContext, ICON_RES,
- notifyText2, notifyTitle2, notifyText2, intent_2, ID_2));
- ...
- mNotificationManager.notify(ID_3,
- genreNotification(mContext, ICON_RES,
- notifyText3, notifyTitle3, notifyText3, intent_3, ID_3));
- [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
- [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
- 同时显示多个Notification时PendingIntent的Intent被覆盖?
- Android问题集锦---显示多个notification时PendingIntent的Intent被覆盖
- 同时显示多个 Notification
- 关于多个应用在同一个server中部署时出现cookies被覆盖的解决方案
- Cocos2d-x中SimpleAudioEngine播放多个背景音乐时被覆盖的问题
- 如何设置 Notification 中PendingIntent 的 Intent
- 如何设置 Notification 中PendingIntent 的 Intent
- 如何设置 Notification 中PendingIntent 的 Intent
- shiro登录,多个项目session被覆盖问题
- 往Vector,ArrayList添加多个对象会被覆盖
- 在notification使用intent时,PendingIntent只是第一次起作用的解决方法
- Notification 中PendingIntent 只打开最后一个Intent的问题
- Android 如何设置 Notification 中PendingIntent 的 Intent
- 重装多系统ubuntu被覆盖后的解决方法
- 解决vc最大化窗口时,任务栏被覆盖的问题
- 哥特人动画版丰田个地方被覆盖
- CwinApp,CView,Cframe之间的关系
- windows环境下定位随机崩溃
- JAVA读取文件名二
- 开心的小明
- 获取元素CSS值之getComputedStyle方法熟悉
- [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
- Android代码混淆第三方jar,can't find referenced class的解决
- JAVA定时任务的简单实现
- Precision and Recall
- android读取大图片并缓存
- 教你如何在一个有500台PC以上的局域网配置IP、子网掩码
- 不一定能写出来的求素数问题
- struts2清除session的方法
- 程序员之路