最近在程序中使用到了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. 【kAri OJ620】winoros的树

    时间限制 1000 ms 内存限制 65536 KB 题目描述 winoros 是一个热爱大自然的萌妹子,在植树节的时候,她打算带着集训的朋友们一起去种树. 到了种树的地方,学校给了她们四个不可弯曲. ...

  2. RBAC(基于角色的访问控制权限)表结构

    Rbac 支持两种类,PhpManager(基于文件的) 和 DbManager(基于数据库的) 权限:就是指用户是否可以执行哪些操作 角色:就是上面说的一组操作的集合,角色还可以继承 在Yii2.0 ...

  3. 【bzoj1042】 HAOI2008—硬币购物

    http://www.lydsy.com/JudgeOnline/problem.php?id=1042 (题目链接) 题意 共有4种硬币,面值分别为c1,c2,c3,c4.某人去商店买东西,去了to ...

  4. 【bzoj1853】 Scoi2010—幸运数字

    http://www.lydsy.com/JudgeOnline/problem.php?id=1853 (题目链接) 今天考试考了容斥,结果空知道结论却不会写→_→ 题意 求区间中不含6,8两个数字 ...

  5. POJ3038 Flying Right

    Description Figuring that they cannot do worse than the humans have, Farmer John's cows have decided ...

  6. asp.net input怎么获取值

    前台: <input type="hidden" name="content" value="content"> 后台: Req ...

  7. Codevs2157 配对

    题目描述 Description 给出2个序列A={a[1],a[2],…,a[n]},B={b[1],b[2],…,b[n]},从A.B中各选出n个元素进行一一配对(可以不按照原来在序列中的顺序), ...

  8. Linux登录验证机制、SSH Bruteforce Login学习

    相关学习资料 http://files.cnblogs.com/LittleHann/linux%E4%B8%AD%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95%E8%AE% ...

  9. 使用U盘安装mint

    系统坏了,重新装的时候,硬盘甚至都没法格式化...所以,狠狠心买了块固态硬盘,123G,威刚. 想自己装Linux系统,这样用起来更方便一点,不用装虚拟机,然后再跑linux什么的.最后选了mint. ...

  10. c++实现gray code(格雷码)

    今天别人问的一道题,强调用分治法实现 =.= 百度了一下格雷码,然后写了一下. 关于格雷码大家看百度的吧,特别详细,贴个图: 代码如下(header_file.h是我自己写的一个头文件,包括常见的ve ...