最近在程序中使用到了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. 缓存插件 EHCache

    EHCache是来自sourceforge(http://ehcache.sourceforge.net/)的开源项目,也是纯Java实现的简单.快速的Cache组件. 下载jar包 Ehcache ...

  2. [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler

    MiniProfiler是Stack Overflow团队设计的一款性能分析的小程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控,监控内容包括数据库内容 ...

  3. javaScript基础练习题-下拉框制作(JQuery)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. CVE: 2014-6271、CVE: 2014-7169 PATCH方案分析

    目录 . RedHat官方给的PATCH第一套方案 . RedHat官方给的PATCH临时方案 . RedHat官方给的PATCH第二套方案 1. RedHat官方给的PATCH第一套方案 0x1: ...

  5. schemaLocation value = 'xxxxxxxxxxxx' must have even number of URI's

    这是因为没有加上Spring的版本号,加上就行了,如: http://www.springframework.org/schema/beans/spring-beans.xsd -3.2.2 http ...

  6. Ubuntu添加开机自动启动程序的方法

    文章出处:http://hi.baidu.com/gcc_gun/blog/item/fe9bbc4b84e911fa83025cb8.html 1. 开机启动时自动运行程序 Linux加载后, 它将 ...

  7. Android日志服务 记录日志

    转: http://easion-zms.iteye.com/blog/981568 import java.io.BufferedReader; import java.io.File; impor ...

  8. strstr()

    char * __cdecl strstr ( const char * str1, const char * str2 ) { char *cp = (char *) str1; char *s1, ...

  9. 织梦DedeCMS调用二级子栏目或者多级栏目解决方法

    本文是关于织梦DedeCMS调用多级子栏目的,拿来测试分享下.DEDECMS v5.7,后台已建栏目目录,如下图1所示: 图1 DEDECMS后台栏目结构 现在,我们先来调用顶级栏目"站长新 ...

  10. SQL防注入程序 v1.0

    /// ***************C#版SQL防注入程序 v1.0************ /// *使用方法: /// 一.整站防注入(推荐) /// 在Global.asax.cs中查找App ...