PendingIntent初步探究

来源:互联网 发布:91取名软件 编辑:程序博客网 时间:2024/06/10 16:45

一、是什么

PendingIntent类提供了一种可以创建由其他应用程序在稍晚的时候来触发的Intent的机制。PendingIntent通常用于包装在响应将来的事件时要触发的Intent。

Intent和PendingIntent的简单区分:

a. Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel
b. Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效
c. PendingIntent自带Context,而Intent需要在某个Context内运行
d. Intent在原task中运行,PendingIntent在新的task中运行

 你可以:

1、通过getActivity(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于启动一个Activity的PendingIntent对象
2、通过getService(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于启动一个Service的PendingIntent对象
3、通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法从系统取得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象

4、也可以通过getActivities(Context context, int requestCode,Intent[] intents, int flags)方法从系统取得一个用于启动一系列Activity的PendingIntent对象(具体看数组Intent[]的元素数量。且此数组中的最后一个元素有特殊要求,因为没有深入研究,这里先不具体展开)

也就是说,使用PendingIntent,你可以将某个Intent交由另外的程序在合适的时候去执行。当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时调用intent的。正由于pendingintent中保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。

二、为什么

PendingIntent的作用已经解释了为什么要有PendingIntent的产生。比如,创建一条通知时

notice.setLatestEventInfo(this"通知""开会啦"PendingIntent.getActivity(this0new Intent(this,Activity2.class), 0));

用户点击通知后执行的动作就是用一个PendingIntent来传递的。

作为初学者,我对PendingIntent的使用还是止步于系统提供的Api。哪些函数的参数中需要使用PendingIntent,我才会去使用它。如果有哪位大神能给讲解一下自定义的函数中使用PendingIntent作为参数的,还请不吝赐教啊!!!


三、怎么用

其实PendingIntent的使用也很简单。既然它是对Intent的包装,那自然少不了要先创建一个Intent实例,然后再根据 (一 、)中讲述的方法中的一种来获取PendingIntent的一个实例就可以了作为参数传递给其他程序使用了。下面就以我应用中的使用来举例说明:

(看过上一篇文章的应该已经有了解了。我的这个应用是一个简单的发短信的应用)

1、

Intent i=new Intent("com.bignerdranch.android.finishtest.SENT_SMS_ACTION");

创建Intent实例。这里传递了一个action给这个Intent。

2、(重点部分

PendingIntent  sentIntent = PendingIntent.getBroadcast(    MainActivity.this,  pendingIntentcount++,   i  ,PendingIntent.FLAG_UPDATE_CURRENT);

获取PendingIntent实例。因为短信发送部分是使用广播接收器来实现短信发送情况(如:是否发送成功)的获取的,所以这里使用了getBroadcast()来获取实例。

这里要解释和注意两个地方:

(1)第二个参数   简单的解释是:Private request code for the sender 即该Intent的发送方的私有请求码。

首先,服务器那边为每一条短信都编了Id,并要求我返回每条短信的发送状态。但我发现如果这个参数简单的设置为一个固定的数值的话,用于反馈短信发送状态的接收器就只能接收到最后一个PendingIntent包含的Intent内的信息。又通过一些上网查询后发现,这个参数似乎能帮助我解决这个问题。其中的一篇文章给我一些启发。既然说这是一个请求码,那跟其它请求码的作用应该是一样的,就是用来做区分的。所以代码中我把发送的PendingIntent数作为requestCode。果然实现了我想要的效果,接收器能够接收到我发出的每一个PendingIntent并分别提取其中的Intent携带的信息。


文章链接如下:http://my.oschina.net/youranhongcha/blog/196933

(2)第四个参数

可以是FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,FLAG_ONE_SHOT 或者任何 Intent.fillIn()支持的flag。

主要就是前四个啦。



对于PendingIntent.getBroadcast()函数:

FLAG_CANCEL_CURRENT(Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.的意思是,当我们获取PendingIntent时,如果可以从总表中查到一个相符的已存在的PendingIntentRecord节点的话,那么需要把这个节点从总表中清理出去,也就是不会创建新的PendingIntent。

而在没有指定FLAG_CANCEL_CURRENT的大前提下,如果用户指定了FLAG_UPDATE_CURRENT标识,那么会用新的intents参数替掉刚查到的PendingIntentRecord中的旧intents。

而不管是刚清理了已存在的PendingIntentRecord,还是压根儿就没有找到符合的PendingIntentRecord,只要用户没有明确指定FLAG_NO_CREATE(Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it.标识,系统就会尽力创建一个新的PendingIntentRecord节点,并插入总表。而指定了FLAG_NO_CREATE的话,如果所描述的PendingIntent不存在则不会创建新的PendingIntent而是直接返回null。

FLAG_ONE_SHOT的意思是Flag indicating that this PendingIntent can be used only once.,即该PendingIntent只能使用一次,一旦被激发使用后机会被清理掉。

3、

smsManager.sendTextMessage(sendTo, null, MessageBody, sentIntent, null);
发送短信,并使用上面获取的PendingIntent实例。
































0 0