PendingIntent是一个Intent的描述、包装,给予了这个PendingIntent 的组件在指定的事件发生或指定的时间到达时启动Activty、Service或者Broadcast。

根据是要启动Activity、Service还是Broadcast分别对应一个获取PendingIntent的方法

public static PendingIntent getActivity(Context context, int requestCode,
            Intent intent, int flags)

public static PendingIntent getBroadcast(Context context, int requestCode
            Intent intent, int flags)

public static PendingIntent getService(Context context, int requestCode,
            Intent intent, int flags)

三个函数的参数都相同,其中最后一个参数flags在文档中是这样解析的:

flags:  May be FLAG_ONE_SHOT,LAG_NO_CREATE,LAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as  supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.

目前为止只提供FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT这四个flag

FLAG_ONE_SHOT:this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
利用 FLAG_ONE_SHOT获取的PendingIntent只能使用一次,即使再次利用上面三个方法重新获取,再使用PendingIntent也将失败。

FLAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.
利用FLAG_NO_CREAT获取的PendingIntent,若描述的Intent不存在则返回NULL值.

FLAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的。你可用使用它去检索新的Intent,如果你只是想改变Intent中的额外数据的话。通过取消先前的Intent,可用确保只有最新的实体可用启动它。如果这一保证不是问题,考虑flag_update_current。

FLAG_UPDATE_CURRENT: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
最经常使用的是FLAG_UPDATE_CURRENT,因为描述的Intent有 更新的时候需要用到这个flag去更新你的描述,否则组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras。

上面4个flag中最经常使用的是FLAG_UPDATE_CURRENT,因为描述的Intent有 更新的时候需要用到这个flag去更新你的描述,否则组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras。
使用 FLAG_CANCEL_CURRENT也能做到更新extras,只不过是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和 FLAG_UPDATE_CURRENT的区别在于能否新new一个Intent,FLAG_UPDATE_CURRENT能够新new一个 Intent,而FLAG_CANCEL_CURRENT则不能,只能使用第一次的Intent。

此外还需要注意参数: 
int requestCode : Private request code for the sender (currently not used).

PendingIntent contentIntent = PendingIntent.getActivity(context,
num, intent, PendingIntent.FLAG_UPDATE_CURRENT);

对于FLAG_UPDATE_CURRENT,如果上面的requestCode 为常量,则对于先后出现的若干Notification,则所有对应的Intent里面的extra被更新为最新的,就是全部同一为最后一次的。
相反,如果num每次不一样,则里面的Inent的数据没被更新。
对于FLAG_CANCEL_CURRENT,则只响应最前面的第一条Notifiacation,后面所有的不响应....

使用请参考:http://txlong-onz.iteye.com/blog/1142737

Android Notification中PendingIntent.Flag的应用相关推荐

  1. 如何设置 Notification 中PendingIntent 的 Intent

    在写完 " androidNotification 的使用 "的时候,发现有几个问题,特别是设置Notification的Intent使之能够像 QQ 或其他程序一样能够正确回调到 ...

  2. Android通知,PendingIntent示例

    Welcome to Android Notification Example using android PendingIntent. In this tutorial we're going to ...

  3. android notification点击无效,Notification.addAction在Android O中无效

    下面提到的代码适用于 android O verison下面的所有设备.对于android O, addAction()方法不起作用,即按钮单击在 Android O中不起作用. 任何帮助,将不胜感激 ...

  4. Android中pendingIntent的深入理解

    pendingIntent字面意义:等待的,未决定的Intent. 要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, ...

  5. [转]Android中pendingIntent的深入理解

    转自;here pendingIntent字面意义:等待的,未决定的Intent. 要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, ...

  6. android notification 的总结分析,Android中Notification用法实例总结

    本文实例总结了 Android中Notification用法.分享给大家供大家参考,具体如下: 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图 ...

  7. Android开发--Notification和PendingIntent

    手机上方的状态栏用于显示通知消息,实现的方法很简单,只需要记住五个步骤即可(步骤写在源代码中的注释中).pendingIntent意思是延期执行的Intent,一般用于Notification中,下面 ...

  8. Android Notification实现推送消息过程中接受到消息端有声音及震动及亮屏提示

    在Android Notification状态栏通知一文中,简单实现了消息的推送效果,这里就接着上文说一下,当用户接受到消息时的提示效果 // 5-加入震动及声音及亮屏 notification.de ...

  9. android Notification的使用

    今天,简单讲讲android里如何使Notification. 之前,我讲如何使用服务器进行版本升级时提到了Notification.这个其实我并不常用,所以当时看代码时也是查找了资料,这个很多地方还 ...

  10. Android Notification通知详解

    Android Notification通知详解 Notification: (一).简介: 显示在手机状态栏的通知.Notification所代表的是一种具有全局效果的通知,程序一般通过Notifi ...

最新文章

  1. 华南主板bios怎么恢复出厂设置_主板电池放电清BIOS恢复出厂设置怎么操作?配图文...
  2. 贫血模型,充血模型(领域驱动设计)
  3. MACD 的数学解释
  4. 小程序页面遮罩且不能滚动 + 内容居中显示
  5. linux分区大容量加入lvm,linux 添加磁盘+lvm扩容
  6. 0305互联网新闻 | 钉钉发布“未来校园”千校计划;海南出台方案鼓励发展网约医疗服务...
  7. 06 | 链表(上):如何实现LRU缓存淘汰算法?
  8. Qt4_子类化QMainWindow
  9. jquery 获取日期时间
  10. DOM之节点操作总结(附实例、图解)
  11. 思科软件服务器怎么作用,TFTP 服务器的选择和使用
  12. 什么原数据更容易平稳_判定数据序列平稳与否的方法都有哪些,什么是平稳序列...
  13. Cximage 库使用,直接读取图像数据到内存。
  14. 【人工智能】180页PPT,讲解人工智能技术与产业发展
  15. 短视频获客系统另附属源码理论分享
  16. echarts x 起始_echarts中如何在dataZoom 最左侧和最右侧始终显示起始和结束的值
  17. 学习微博中情感分类的句子表达(NLPCC2013)
  18. Hutool工具生成二维码
  19. 【PSO三维路径规划】粒子群算法融合鸡群算法多无人机三维路径规划【含Matlab源码 1792期】
  20. 第七讲:1.物联网敲击桌面打开小台灯

热门文章

  1. eclipse 装阿里规范模板
  2. 如何在PDF文件中提取图片?PDF图片提取教程
  3. Bayesian framework 贝叶斯框架 (R)
  4. win10电脑怎么将html网页做成壁纸,手把手教你win10动态桌面怎么设置
  5. STM32驱动步进电机;步进电机的驱动;步进电机驱动板的使用;STM32输出不同频率的波形;
  6. adb shell 小米手机_小米手机ADB删除系统应用去广告。
  7. 改变世界的17个数学公式
  8. 英文书信开头结尾模板
  9. 每日学习, 特征方程的复根
  10. Matlab基本操作与矩阵输入