首页 > 移动平台 > 详细

android 多个notifycation向同一个Actiivity传递不同数据

时间:2014-07-11 23:15:37      阅读:926      评论:0      收藏:0      [点我收藏+]

如果你有这方面的需求,那你实践的时候可能会发现,多个Notifycation点击的时候会传递相同的数据.

通常情况下我们可能这样写.

Notification notification = new Notification(R.drawable.ic_launcher_9, name , System.currentTimeMillis());

Intent intent = new Intent(mContext , ThemeInfo.class);
Bundle bundle = new Bundle();
bundle.putString("apkid", packageName);
bundle.putBoolean("isApplied", false);
intent.putExtra("bundle", bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

notification.setLatestEventInfo(mContext, name, "hello", pendingIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;

Log.d("NewThemeChooser__:ThemeChangeReceiver" , "hascode : " + packageName.hashCode() + " installed " + packageName);
notificationManager.notify(packageName.hashCode(), notification);

什么原因呢?

答案就在红色字体部分上。

个人认为是这样的: notifycation点击后整个intent都会通过parcel序列化到内存里。

而pendingIntent的第2个参数requestCode如果相同,数据就会被覆盖。导致传递的数据相同了。

怎么解决呢?

也很简单

PendingIntent pendingIntent = PendingIntent.getActivity(mContext, packageName.hashCode(), intent, 0);

也就是说第2个参数在不同的notifycation中一定要唯一。

 

答案实际上也是我在stackoverflow上找到的. 如果你遇到问题想快速解决的话,stackoverflow是个不错的地方。

android 多个notifycation向同一个Actiivity传递不同数据,布布扣,bubuko.com

android 多个notifycation向同一个Actiivity传递不同数据

原文:http://www.cnblogs.com/don-insist/p/3832985.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!