Android 中PendingIntent---附带解决AlarmManager重复加入问题
最近在程序中使用到了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掉。在调用效果上,getActivity和Context.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重复加入问题的更多相关文章
- Android中pendingIntent的深入理解
pendingIntent字面意义:等待的,未决定的Intent.要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, i ...
- 关于android中PendingIntent.getBroadcase的注册广播
使用语句 PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent inten ...
- 《安卓网络编程》之第六篇 Android中的WIFI和蓝牙
关于WIFI就不多介绍啦,直接来个段子吧. 问:“WiFi对人体有伤害么?” 答:“不清楚,反正没有WiFi我就浑身不舒服. 比较重要的一点就是WifiManager wm=(WifiManager ...
- Android中使用AlarmManager进程被删除的解决办法
http://blog.csdn.net/zhouzhiwengang/article/details/13022325 在Android中,AlarmManager提供了不受休眠状态的系统定时功能, ...
- 【转】android中重复连接ble设备导致的连接后直接返回STATE_DISCONNECTED的解决办法---不错不错,重新连接需要花费很长的时间
原文网址:http://bbs.eeworld.com.cn/thread-438571-1-1.html /* * 通过使用if(gatt==null ...
- Android中AlarmManager使用示例(持续更新,已经更改)
现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现.AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服 ...
- 转:在Android中使用AlarmManager
原地址http://blog.csdn.net/maosidiaoxian/article/details/21776697 AlarmManager是Android中的一种系统级别的提醒服务,它会为 ...
- Mono For Android中AlarmManager的使用
最近做了一个应用,要求如下: 程序运行之后的一段时间,分别触发3个不同的事件.当然很快就想到了Android中的AlarmManager和BroadcastReceiver.但是毕竟Mono环境和Ja ...
- 在Android中使用AlarmManager
AlarmManager是Android中的一种系统级别的提醒服务,它会为我们在特定的时刻广播一个指定的Intent.而使用Intent的时候,我们还需要它执行一个动作,如startActivity, ...
随机推荐
- hdu4825 字典树 XOR
用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...
- Spring 作用域 scope
spring的作用域将对Bean的生命周期和创建方式产生影响. 主要分为五种类型的作用域 singleton (默认)在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. ...
- 【CodeForces 557B】Pasha and Tea
题 题意 总共有 w 克蛋糕,2n 个盘子,第 i 个盘子容量为 ai ,n 个女孩和 n 个男孩,男孩得到的是女孩得到的蛋糕的两倍,求他们得到蛋糕的最大值. 分析 把盘子从小到大排序,然后 女生得到 ...
- ios学习笔记
1.对于autorelease的理解 Each thread in a Cocoa application maintains its own stack of autorelease pool bl ...
- Windows下一些配置信息
VC.VS和.NetFramework版本对应关系 VC6.0对应VS 6.0 VC7.0对应VS 2002 VC7.1对应VS 2003 .Net Framework1.0/1.1 VC8.0对应V ...
- Linux中的TUN/TAP设备
今天才发现这家伙...怎么讲...深以为耻.晚上的任务是加深对它的了解,就这么定了. 1. General questions.1.1 What is the TUN ? The TUN is Vi ...
- 使用multi curl进行http并发访问
curl是一款利用URL语法进行文件传输的工具,它支持多种协议,包括FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET等,我们既可以在命令行上使用它,也可以利用 libcur ...
- 快速上手如何使用FluentData
http://blog.itpub.net/29511780/viewspace-1194048/ 目录: 一.什么是ORM? 二.使用ORM的优势 三.使用ORM的缺点 四.NET下的ORM框架有 ...
- --hdu 1050 Moving Tables(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...
- C++中 :: 的意思
表示作用域,和所属关系 ::是运算符中等级最高的,它分为三种:1)global scope(全局作用域符),用法(::name)2)class scope(类作用域符),用法(class::name) ...