PendingItent

来源:互联网 发布:淘宝店铺怎样引流量 编辑:程序博客网 时间:2024/06/04 23:27

用PendingItent来执行目标动作和intent。(A description of an Intent and target action to perform with it)这个类的实例通过调用 getActivity(Context, int, Intent, int),getActivities(Context, int, Intent[], int)getBroadcast(Context, int, Intent, int)  getService(Context, int, Intent, int)获得。返回的对象可以传递给其他的应用,以便他们可以随后执行你希望那个的动作.

通过把PendingIntent给其他的应用,代表你授权给给其它应用执行你指定的操作,好像其他的应用程序是你自己的(有相同的权限)因此你必须小心的构造PendingItent:通常这个base intent 应该有一个明确的组件名,这个组件名应该从属与你自己,确保最终会发送到指定的组件,并且只到那里。

PendingIntent是一个被系统约束用来描述原始数据的引用,通过这个数据获取到PendingIntent。(A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it.)这也就是说,即便拥有它的进行killed,PendingIntent本身仍旧在接收它的进程中是有效的。如果创建的应用程序重新获取了相同类别的PendingIntent(相同的操作,相同的intent 动作,数据,类别和组件、相同的标记),它可以重新获取一个代表相同内容的PendingIntent(如果PendingIntent还有效),并且可以通过调用 cancel()  取消掉它。

应为这个行为特性,知道什么时候两个Intent被用来考虑为获得PendingItent具有相同目的是非常重要的.(Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent) 人们容易犯的一个常规动作是用几个只是在参数方面不同的intent,来期望每次获取不同的PendingIntent,但这这不会发生。用来匹配Intent是否相同的部分是定义在 Intent.filterEquals 中的部分。如果你的两个Intent对象在 Intent.filterEquals 是相同的,你会获得两个相同的PendingIntent。

这样处理有两个典型的原因。

如果你真的需要多个不同的PendingIntent对象在同意时间(例如,需要在同一时间显示两个不同的通知)。那么你必须要确保PendingIntent是有部分不同的。可以是intent中的Intent.filterEquals  属性不同,或者是 getActivity(Context, int, Intent, int),getActivities(Context, int, Intent[], int)getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int) 中的request code 不同。 

如果你需要对任意的Intents都使用相同的PendingIntent,你可以选择 FLAG_CANCEL_CURRENT orFLAG_UPDATE_CURRENT 来取消或修当前的PendingIttent。