• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

MoonShine

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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

如果你有这方面的需求,那你实践的时候可能会发现,多个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是个不错的地方。

====================== 养成写博客的好习惯 ============================

posted on 2014-07-09 09:55  MoonShine  阅读(495)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3