转http://harvey8819.blog.163.com/blog/static/162365181201132691559986/

今天真算是吃尽了苦头,往Android的notifications里增加一条记录的时候,如果只需要一条通知无问题,但如果根据不同情况添加不同的通知,我们知道可通过方法notificationManager.notify(id,notification)的id参数指定不同的id就行了,但事实上通常会出现以下问题:我们一般会通过一个PendingIntent指定一个Intent,使点击notification后跳转到指定的视图,可是奇怪的是,当我添加了两条以上通知并通过Bundle传送了一些数据之后,后面传的Bundle中的数据会把前面的覆盖掉,也就是说无论添加了多少条通知,点击跳转过去的视图都变成一样的了。百思不得其解。

搜了大量资料后,才偶然发现是PendingIntent 的问题:

PendingIntent 需要通过以下方法取得:PendingIntent.getActivity (Context context, int requestCode, Intent intent, int flags),看sdk,对这个方法这样描述:

public staticPendingIntentgetActivity(Contextcontext, int requestCode,Intentintent, int flags)

Since: API Level 1

Retrieve a PendingIntent that will start a new activity, like callingContext.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use theIntent.FLAG_ACTIVITY_NEW_TASKlaunch flag in the Intent.

Parameters
context The Context in which this PendingIntent should start the activity.
requestCode Private request code for the sender (currently not used).
intent Intent of the activity to be launched.
flags May beFLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn()to control which unspecified parts of the intent that can be supplied when the actual send happens.
Returns
  • Returns an existing or new PendingIntent matching the given parameters. May return null only ifFLAG_NO_CREATEhas been supplied.

我们看到requestCode打了括号说明是currently not used,既然还没使用,我就像网上很多例子一样随便给一个0就算了。可实际上这个参数是有用的:当同时有多个通知的时候,可通过这个参数来互相区别,否则的话,就像我开始遇到的情况,后加进去的Intent会把前面的盖掉。弄清楚了这点,事情就好办了,我把PendingIntent.getActivity的requestCode参数设成了与noti

更多相关文章

  1. android 中关于SimpleAdapter构造参数的问题
  2. Android应用之PopupWindow显示位置详解
  3. ProGuard详解
  4. android 中关于SimpleAdapter构造参数的问题。
  5. 记录Activity的onCreate()方法的参数PersistableBundle
  6. [Android] JNI 中传递字符串参数(String)的安全处理方式
  7. Android(安卓)MediaPlayer指定文件位置播放
  8. Android:指定某浏览器访问指定页面
  9. 使用 SQLiteDatabase 操作 SQLite 数据库

随机推荐

  1. EditText属性简介
  2. Android与webview JS 键值编码差异
  3. Android Studio快捷键、配置 Android Stu
  4. Android NKD环境搭建 若干问题.
  5. Android share绘制虚线在手机上显示实线
  6. Linux Kernel and Android休眠与唤醒
  7. Using Ant to Automate Building Android
  8. 前言:Android进阶汇总(持续更新)
  9. 【Android】入门级连接网络示例: 网页浏览
  10. Android 学习记录-ImageView显示格式