最近在程序中使用到了notification功能,自然,就涉及到了PendingIntent,下面总结下。

1 什么是PendingIntent

A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int),getActivities(Context, int, Intent[], int)getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

简单的说,就是一个不立即执行的intent。虽然这里函数形式是getActivity,但这个函数的效果不仅仅是获得了一个对象,正如官网文档对这个函数的介绍     Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).      这个函数调用后,相应的pendingIntent就会在系统中注册,等待调用。如果想取消注册,必须在创建它的程序中cancel掉。在调用效果上,getActivityContext.startActivity(Intent)比,仅仅是延缓了执行。

2 如何判断PendingIntent相同

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does nothappen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as perIntent.filterEquals, then you will get the same PendingIntent for both of them.

There are two typical ways to deal with this.

If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered byIntent.filterEquals, or different request code integers supplied to getActivity(Context, int, Intent, int)getActivities(Context, int, Intent[], int)getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

If you only need one PendingIntent active at a time for any of the Intents you will use, then you can alternatively use the flags FLAG_CANCEL_CURRENT orFLAG_UPDATE_CURRENT to either cancel or modify whatever current PendingIntent is associated with the Intent you are supplying.

简单的说,要想要2个不同的PendingIntent,有2个办法

A.用不同的intent(相同的intent指的是:same operation, same Intent action, data, categories, and components, 其实,action, data, categories, and components这4个属性都是intent的过滤条件,当然要一样才行!),特别注意只更改"extra contents“没用。

B.用不同的requestCode。就是getActivity(Context, int, Intent, int)的第二个参数。

3.创建时可以用的flag(就是getActivity(Context, int, Intent, int)的第四个参数)

一般就4个

FLAG_CANCEL_CURRENT

请参见文档

FLAG_NO_CREATE

这个可以用来验证指定的pendingIntent是否已经存在于系统中了,在我写的程序里这样用

Intent temIntent = new Intent("notificationCheck");
PendingIntent oldSender = PendingIntent.getBroadcast(context, 0, temIntent, PendingIntent.FLAG_NO_CREATE);
if(oldSender != null)
{
return;
}

Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);

PendingIntent operation = PendingIntent.getBroadcast(context, 0, temIntent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000*60*15, operation);

这样就保证了,不会重复向系统中注册alarm(解决的问题:每次注册都会导致alarm立即响应一次,即使不是设置的时间,不明白为什么))。

其实这里没有直接对AlarmManager的信息进行验证,仅仅是通过验证pendingIntent是否在系统注册过,来间接检验是否已经加过了alarm。

FLAG_ONE_SHOT

请参见文档

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.

当仅仅更换intent中的extra数据时,就用这个。

Android 中PendingIntent---附带解决AlarmManager重复加入问题的更多相关文章

  1. Android中pendingIntent的深入理解

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

  2. 关于android中PendingIntent.getBroadcase的注册广播

    使用语句 PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent inten ...

  3. 《安卓网络编程》之第六篇 Android中的WIFI和蓝牙

    关于WIFI就不多介绍啦,直接来个段子吧. 问:“WiFi对人体有伤害么?” 答:“不清楚,反正没有WiFi我就浑身不舒服. 比较重要的一点就是WifiManager  wm=(WifiManager ...

  4. Android中使用AlarmManager进程被删除的解决办法

    http://blog.csdn.net/zhouzhiwengang/article/details/13022325 在Android中,AlarmManager提供了不受休眠状态的系统定时功能, ...

  5. 【转】android中重复连接ble设备导致的连接后直接返回STATE_DISCONNECTED的解决办法---不错不错,重新连接需要花费很长的时间

    原文网址:http://bbs.eeworld.com.cn/thread-438571-1-1.html /*                         * 通过使用if(gatt==null ...

  6. Android中AlarmManager使用示例(持续更新,已经更改)

    现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现.AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服 ...

  7. 转:在Android中使用AlarmManager

    原地址http://blog.csdn.net/maosidiaoxian/article/details/21776697 AlarmManager是Android中的一种系统级别的提醒服务,它会为 ...

  8. Mono For Android中AlarmManager的使用

    最近做了一个应用,要求如下: 程序运行之后的一段时间,分别触发3个不同的事件.当然很快就想到了Android中的AlarmManager和BroadcastReceiver.但是毕竟Mono环境和Ja ...

  9. 在Android中使用AlarmManager

    AlarmManager是Android中的一种系统级别的提醒服务,它会为我们在特定的时刻广播一个指定的Intent.而使用Intent的时候,我们还需要它执行一个动作,如startActivity, ...

随机推荐

  1. UTL_FILE详解

    包UTL_FILE 提供了在操作系统层面上对文件系统中文件的读写功能.非超级用户在使用包UTL_FILE中任何函数或存储过程前必须由超级用户授予在这个包上的EXECUTE权限.例如:我们使用下列命令对 ...

  2. SqlServer建表规范

    一.数据库在建表时,一般默认字段如下,也算是标准字段吧 删除标志:DeletionStateCode 创建时间:CreateOn 创建人:CreateBy 更新时间:ModifiedOn 更新人:Mo ...

  3. linux内核数据结构学习总结

    目录 . 进程相关数据结构 ) struct task_struct ) struct cred ) struct pid_link ) struct pid ) struct signal_stru ...

  4. Php学习之SESSION反序列化机制

    在php.ini中存在三项配置项:session.save_path="" --设置session的存储路径session.save_handler="" -- ...

  5. iOS 知识点梳理

    OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...

  6. POJ1745Divisibility(01背包思想)

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11151   Accepted: 3993 Des ...

  7. Java线程中run和start方法的区别

    http://bbs.csdn.net/topics/350206340 Thread类中run()和start()方法的区别如下:run()方法:在本线程内调用该Runnable对象的run()方法 ...

  8. Spring学习8-Spring事务管理(注解式声明事务管理)

    步骤一.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/beans& ...

  9. 【2011图灵奖得主】我眼中的Judea Pearl

    [2011图灵奖得主]我眼中的Judea Pearl 来源: 叶星遥的日志 2011年的图灵奖花落UCLA计算机系的Judea Pearl教授.图灵奖是计算领域的最高奖,由于近年来这个领域的兴盛也算是 ...

  10. CentOS 设置网络(修改IP&修改网关&修改DNS)--update.14.08.15

    自己电脑上装的虚拟机用桥接方式连接物理机,虚拟机重启后ip会发生变化,非常阻碍Xshell的连接和hosts指定的dns. 通过修改IP为static模式,保持IP不变. ============== ...