Android开发中,PendingIntent主要用于NotificationAlarmManager以及Widget中,获取PendingIntent主要有三种方式:getActivity()getService()以及getBroadcast(),这三种方式的参数都相同,但其中的第2个参数requestCode和第4个参数flag却不太好理解,这里结合Notification中的PendingIntent进行说明。

首先要明确一点,什么样的PendingIntent能够算是同一个PendingIntent。根据Google文档对PendingIntent的描述,当两个PendingIntent的类型为同一个(即两个同为getActivity()getService()getBroadcast()获取的)时,并且Intentdataactioncomponentcategoryflag相同时(特别注意,Intentextra不算),两个PendingIntent算是同一个。此外,第二个参数requestCode也可用来区分PendingIntent,因此即使两个PendingIntent的类型相同并且Intent相同,但如果requestCode不同的话,也算是两个不同的PendingIntent明确上述后,还需对flag进行说明,这里主要讲解FLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT

FLAG_CANCEL_CURRENT:若该PendingIntent已存在,则取消之前的PendingIntent,同时生成新的PendingIntent

FLAG_UPDATE_CURRENT:若该PendingIntent已存在,则将该PendingIntentIntentextra数据更新为最新的。

因此,若一个Notification中指定了多个PendingIntentdeleteIntentcontentIntentsetOnClickPendingIntent),或多个Notification中都指定了PendingIntent,则如果存在相同的PendingIntent,则对于flagFLAG_CANCEL_CURRENT时,之前的PendingIntent将被取消,Intent的内容无法传递,当前的PendingIntent不受影响;对于flagFLAG_UPDATE_CURRENT时,该PendingIntentIntentextra数据将被更新为本次最新的,则之前的PendingIntentextra数据被修改为本次最新的。


例一:

Intentintent1=newIntent(mContext,NotificationService.class);

intent1.putExtra("id",1);

PendingIntentdeleteIntent=PendingIntent.getService(mContext,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);

notification.deleteIntent=deleteIntent;


Intentintent2=newIntent(mContext,NotificationService.class);

intent2.putExtra("id",2);

PendingIntentcontentIntent=PendingIntent.getService(mContext,0,intent2,PendingIntent.FLAG_UPDATE_CURRENT);

notification.contentIntent=contentIntent;

说明:contentIntentdeleteIntent类型相同(都为通过getService()获取),同时Intent也相同,并且requestCode也相同,因此认为是同一个PendingIntent。由于contentIntent后定义,并且flagFLAG_UPDATE_CURRENT,因此会将deleteIntentextra修改为contentIntentextra,即将deleteIntentid修改为2


例二:

Intentintent1=newIntent(mContext,NotificationService.class);

intent1.setAction(“intent1”);

intent1.putExtra("id",1);

PendingIntentdeleteIntent=PendingIntent.getService(mContext,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);

notification.deleteIntent=deleteIntent;


Intentintent2=newIntent(mContext,NotificationService.class);

intent2.setAction(“intent2”);

intent2.putExtra("id",2);

PendingIntentcontentIntent=PendingIntent.getService(mContext,0,intent2,PendingIntent.FLAG_UPDATE_CURRENT);

notification.contentIntent=contentIntent;

说明:由于intent1intent2action不同,因此deleteIntentcontentIntent不同,它们的extra互不影响。


例三:

Intentintent1=newIntent(mContext,NotificationService.class);

intent1.putExtra("id",1);

PendingIntentdeleteIntent=PendingIntent.getService(mContext,1, intent1,PendingIntent.FLAG_UPDATE_CURRENT);

notification.deleteIntent=deleteIntent;


Intentintent2=newIntent(mContext,NotificationService.class);

intent2.putExtra("id",2);

PendingIntentcontentIntent=PendingIntent.getService(mContext,2,intent2,PendingIntent.FLAG_UPDATE_CURRENT);

notification.contentIntent=contentIntent;

说明:由于requestCode不同,因此deleteIntentcontentIntent不同,它们的extra互不影响。

更多相关文章

  1. map 参数学习
  2. android+django交互数据(同时上传图片与文字)
  3. 两个模拟器之间对发短信
  4. XDebuggable&mprop代码分析
  5. Android(安卓)Studio 编译时报错:Duplicate files copied in APK
  6. 安卓动画(Animation使用)
  7. android布局文件 在使用singleLine时遇到的问题
  8. Android两个Activity传递数据,onActivityResult获取结果时Intent
  9. android 数据持久化——I/O操作

随机推荐

  1. Android实现word模板套打功能
  2. 【android】应用程序签名详解
  3. Android网络编程之通过Post传递参数
  4. Android(安卓)面试题1
  5. Android中的GraphicBuffer同步机制-Fence
  6. Google 官方推出应用开发架构指南
  7. Andoid Studio-android开发03-点击按钮Bu
  8. Android(安卓)JNI 动态注册方法(JNI_OnLoa
  9. android 动态修改 TextView的width属性
  10. Android设计模式系列(7)--SDK源码之命令