首页 > 其他分享 >PendingIntent 详解

PendingIntent 详解

时间:2022-12-18 19:33:44浏览次数:83  
标签:int FLAG 详解 Intent Context intent PendingIntent


public final class



PendingIntent



extends Object



implements Parcelable


简介



PendingIntent用于描述Intent及其最终的行为. 



你可以通过 ​​getActivity​​​(​​Context​​​ context, int requestCode, ​​Intent​​ intent, int flags) 系列方法从系统取得一个 用于启动一个Activity的PendingIntent对象 ,



可以通过 ​​getService​​​(​​Context​​​ context, int requestCode, ​​Intent​​ intent, int flags) 方法从系统取得一个 用于启动一个Service的PendingIntent对象



可以通过 ​​getBroadcast​​​(​​Context​​​ context, int requestCode, ​​Intent​​ intent, int flags) 方法从系统取 得一个用于向BroadcastReceiver的Intent广播的PendingIntent对象



返回的PendingIntent可以递交给别的应用程序,然后继续处理。这里的话你可以稍后才处理PendingIntent中描述的Intent及其最终行为。



当你把 PendingIntent 递交给别的程序进行处理时,PendingIntent仍然拥有PendingIntent原程序所拥有的权限(with the same permissions and identity). 当你从系统取得一个PendingIntent时,一定要非常小心才行。比如,通常,如果Intent目的地是你自己的component( Activity / Service/BroadcastReceiver )的话,你最好采用在Intent中显示指定目的component名字的方式,以确保Intent最终能发到目的,否则Intent最后可能不知道发到哪里了 。一个 PendingIntent 就是Android系统中的一个token(节点,这个应该是Linux或C\C++用语)的一个对象引用,它描述了一些将用于retrieve的数据(这里,这些数据描述了Intent及其最终的行为)。



这就意味着即使PendingIntent原进程结束了的话, PendingIntent 本身仍然还存在,可在其他进程(PendingIntent被递交到的其他程序)中继续使用. 如果我在从系统中提取一个 PendingIntent 的,而系统中有一个和你描述的 PendingIntent 对等的 PendingInent , 那么系统会直接返回和该PendingIntent其实是同一token的 PendingIntent ,而不是一个新的 token 和 PendingIntent 。然而你在从提取 PendingIntent 时,通过 FLAG_CANCEL_CURRENT参数 ,让这个老 PendingIntent 的先cancel()掉,这样得到的 pendingInten 和其 token 的就是新的了。



通过 FLAG_UPDATE_CURRENT参数 的话,可以让新的Intent会更新之前 PendingIntent 中的Intent对象数据,例如更新Intent中的Extras 。另外,我们也可以在 PendingIntent 的原进程中调用PendingIntent的cancel ()把其从系统中移除掉。




注意: 两个PendingIntent对等是指它们的operation一样, 且其它们的Intent的action, data, categories, components和flags都一样。 但是它们的Intent的Extra可以不一样 。


主要常量


FLAG_CANCEL_CURRENT: 如果当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的PendingIntent取消,然后重新生成一个 PendingIntent 对象。



FLAG_NO_CREATE: 如果当前系统中不存在相同的 PendingIntent 对象,系统将不会创建该 PendingIntent 对象而是直接返回null。



FLAG_ONE_SHOT: 该PendingIntent只作用一次。在该 PendingIntent 对象通过 send() 方法触发过后, PendingIntent 将自动调用 cancel() 进行销毁,那么如果你再调用 send() 方法的话,系统将会返回一个 SendIntentException 。



FLAG_UPDATE_CURRENT: 如果系统中有一个和你描述的 PendingIntent 对等的 PendingInent ,那么系统将使用该 PendingIntent 对象,但是会使用新的Intent来更新之前 PendingIntent 中的 Intent 对象数据,例如更新Intent中的Extras。


主要成员函数


getActivities系列方法


该 系列方法将从系统取得一个 用于启动一个 Activity 的PendingIntent对象。


public static ​​PendingIntent​​​ getActivity (​​Context​​​ context, int requestCode, ​​Intent​​ intent, int flags)



Since:  ​​API Level 1​


​Context.startActivity(Intent)​​. Note that the activity will be started outside of the context of an existing activity, so you must use the ​​Intent.FLAG_ACTIVITY_NEW_TASK​

因为对于Context的startActivity方法,如果不是在其子类(Activity)中调用,那么必须对Intent加上FLAG_ACTIVITY_NEW_TASK。


具体可以参照​​Context​​​中对startActivity方法的说明或

《 ​​​Activity和Task的基本模型​


Parameters

context

The Context in which this PendingIntent should start the activity.

requestCode

currently not used).

intent

Intent of the activity to be launched.

flags

​FLAG_ONE_SHOT​​, ​​FLAG_NO_CREATE​​, ​​FLAG_CANCEL_CURRENT​​, ​​FLAG_UPDATE_CURRENT​​, or any of the flags as supported by​​Intent.fillIn()​​ to control which unspecified parts of the intent that can be supplied when the actual send happens.

当我们使用Intent.fillIn()方法时,表示其Intent的某个数据项可以被send方法的Inent参数进行覆盖重写。



Returns


​FLAG_NO_CREATE​

public static ​​PendingIntent​​​ getActivities (​​Context​​​ context, int requestCode, ​​Intent[]​​ intents, int flags)



Since:  ​​API Level 11​


​getActivity(Context, int, Intent, int)​​, but allows an array of Intents to be supplied. The first Intent in the array is taken as the primary key for the PendingIntent, like the single Intent given to ​​getActivity(Context, int, Intent, int)​​. Upon sending the resulting PendingIntent, all of the Intents are started in the same way as they would be by passing them to ​​startActivities(Intent[])​​. The first intent in the array will be started outside of the context of an existing activity, so you must use the ​​Intent.FLAG_ACTIVITY_NEW_TASK​​ The last intent in the array represents the key for the PendingIntent. In other words, it is the significant element for matching (as done with the single intent given to​​getActivity(Context, int, Intent, int)​​, its content will be the subject of replacement by ​​send(Context, int, Intent)​​ and​​FLAG_UPDATE_CURRENT​​, etc. This is because it is the most specific of the supplied intents, and the UI the user actually sees when the intents are started.



Parameters

context

The Context in which this PendingIntent should start the activity.

requestCode

Private request code for the sender (currently not used).

intents

Array of Intents of the activities to be launched.

flags

​FLAG_ONE_SHOT​​, ​​FLAG_NO_CREATE​​, ​​FLAG_CANCEL_CURRENT​​, ​​FLAG_UPDATE_CURRENT​​, or any of the flags as supported by​​Intent.fillIn()​​ to control which unspecified parts of the intent that can be supplied when the actual send happens.

当我们使用Inent.fillIn()所支持的flags时,表示其Intent的数据项可以被send方法的Intent参数覆盖重写。



Returns

​FLAG_NO_CREATE​

getService方法


该方法将系统取得一个 用于启动一个 Service 的PendingIntent对象.


public static ​​PendingIntent​​​ getService (​​Context​​​ context, int requestCode, ​​Intent​​ intent, int flags)



Since:  ​​API Level 1​


​Context.startService()​​. The start arguments given to the service will come from the extras of the Intent.


Parameters

context

The Context in which this PendingIntent should start the service.

requestCode

Private request code for the sender (currently not used).

intent

An Intent describing the service to be started.

flags

​FLAG_ONE_SHOT​​, ​​FLAG_NO_CREATE​​, ​​FLAG_CANCEL_CURRENT​​, ​​FLAG_UPDATE_CURRENT​​, or any of the flags as supported by​​Intent.fillIn()​​ to control which unspecified parts of the intent that can be supplied when the actual send happens.

当我们使用Intent.fillIn()方法的flags时,它表示其Intent的数据项可以被send方法的Intent参数覆盖重写。



Returns

​FLAG_NO_CREATE​

getBroadcast方法


该方法将从系统取 得一个用于向 BroadcastReceiver 的Intent广播的PendingIntent对象


public static ​​PendingIntent​​​ getBroadcast (​​Context​​​ context, int requestCode, ​​Intent​​ intent, int flags)



Since:  ​​API Level 1​


​Context.sendBroadcast()​​.


Parameters

context

The Context in which this PendingIntent should perform the broadcast.

requestCode

Private request code for the sender (currently not used).

intent

The Intent to be broadcast.

flags

​FLAG_ONE_SHOT​​, ​​FLAG_NO_CREATE​​, ​​FLAG_CANCEL_CURRENT​​, ​​FLAG_UPDATE_CURRENT​​, or any of the flags as supported by​​Intent.fillIn()​​ to control which unspecified parts of the intent that can be supplied when the actual send happens.

当我们使用Intent.fillIn()方法的flags时,它表示其Intent的数据项可以被send方法的Intent参数覆盖重写。



Returns

​FLAG_NO_CREATE​

send系列方


该系列主要用于触发 PendingIntent的 Intent行为。用其Intent或启动一个Activity,或启动一个Service,或向BroadcastReceiver发送Intent广播。


public void send ()



Since:  ​​API Level 1​



Perform the operation associated with this PendingIntent.



Throws

​PendingIntent.CanceledException​

Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.


public void send (​​Context​​​ context, int code, ​​Intent​​ intent)



Since:  ​​API Level 1​



Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use.



Parameters

context

The Context of the caller.

该参数是因为intent参数才需要提供的,所用如果你的intent参数不为null的话,该参数也不能为null.

code

Result code to supply back to the PendingIntent's target.

intent

​Intent.fillIn()​



Throws

​PendingIntent.CanceledException​

Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.


public void send (int code, ​​PendingIntent.OnFinished​​​ onFinished, ​​Handler​​ handler)



Since:  ​​API Level 1​



Perform the operation associated with this PendingIntent, allowing the caller to be notified when the send has completed.



Parameters

code

Result code to supply back to the PendingIntent's target.

onFinished

The object to call back on when the send has completed, or null for no callback.

通过该参数,我们可以设置在Intent发送成功后的回调函数。

handler

Handler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process.

用于说明onFinished参数指定的回调函数,最终在哪个Handler中进行调用。



Throws

​PendingIntent.CanceledException​

Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.


public void send (​​Context​​​ context, int code, ​​Intent​​​ intent, ​​PendingIntent.OnFinished​​​ onFinished, ​​Handler​​ handler)



Since:  ​​API Level 1​



Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use and be notified when the send has completed.

​getActivity(Context, int, Intent, int)​​, ​​getBroadcast(Context, int, Intent, int)​​, or ​​getService(Context, int, Intent, int)​​.


Parameters

context

intent is also null.

该参数是因为intent参数才需要提供的,一般是当前的context,如果你的intent参数不为null的话,该函数也不能为null.

code

Result code to supply back to the PendingIntent's target.

intent

​Intent.fillIn()​

onFinished

The object to call back on when the send has completed, or null for no callback.

通过该参数,我们可以指定Intent发送成功后的回调函数。

handler

Handler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process.

该参数说明onFinished参数指定的回调函数将在哪个Handler中进行调用。



Throws

​PendingIntent.CanceledException​

Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.


cancel()函数


public void cancel ()



Since:  ​​API Level 1​



Cancel a currently active PendingIntent. Only the original application owning an PendingIntent can cancel it.

只有PengdingIntent的原应用程序才能调用cancel()来把它从系统中移除掉。


getTargetPackage()函数

public ​​String​​ getTargetPackage ()



Since:  ​​API Level 1​



Return the package name of the application that created this PendingIntent, that is the identity under which you will actually be sending the Intent. The returned string is supplied by the system, so that an application can not spoof its package.



Returns
  • The package name of the PendingIntent, or null if there is none associated with it.

标签:int,FLAG,详解,Intent,Context,intent,PendingIntent
From: https://blog.51cto.com/u_15907753/5950987

相关文章

  • Git应用开发详解之Git入门指引
    Git简史官方网站:https://git-scm.com/​​Linux内核一开始使用BitKeeper来管理和维护代码。2005年,BitKeeper不再免费使用,Linux开源社区需要一套自己的版本控......
  • Git应用开发详解之Git入门指引
    Git简史官方网站:https://git-scm.com/​​Linux内核一开始使用BitKeeper来管理和维护代码。2005年,BitKeeper不再免费使用,Linux开源社区需要一套自己的版本控......
  • Git应用开发详解之Git入门指引
    Git简史官方网站:https://git-scm.com/​​Linux内核一开始使用BitKeeper来管理和维护代码。2005年,BitKeeper不再免费使用,Linux开源社区需要一套自己的版本控......
  • Git应用开发详解之Git入门指引
    Git简史官方网站:https://git-scm.com/​​Linux内核一开始使用BitKeeper来管理和维护代码。2005年,BitKeeper不再免费使用,Linux开源社区需要一套自己的版本控......
  • WebApi 接口参数不再困惑:传参详解
    从网上看了WEBAPI理解感觉是不错的分享一下前言:还记得刚使用WebApi那会儿,被它的传参机制折腾了好久,查阅了半天资料。如今,使用WebApi也有段时间了,今天就记录下API接口传参......
  • matplotlib绘图详解
    目录matplotlib绘图详解一、基础准备1、matplotlib简介2、环境准备2.1安装2.2参考文档3、图表结构4、常用图表二、绘图步骤1、常用图表1.1散点图1.2柱状图1.3饼......
  • MyBatis核心配置文件详解
    目录environmentstransactionManagerDataSource引入jdbc.propertiestypeAliasessettings下划线转驼峰延迟加载MappersIDEA核心配置模板及解释environments可以配置多个......
  • DBGridEh全部属性设置详解
    DBGridEh全部属性设置详解 属性子选项 值 说明Allowedoperations[alopInsertEh,alopUpdateEh,alopDeleteEh,alopAppendEh]允许操作类型:插入、更新、删除、追加Al......
  • Pytorch框架详解之一
    Pytorch基础操作numpy基础操作定义数组(一维与多维)寻找最大值维度上升与维度下降数组计算矩阵reshape矩阵维度转换代码实现importnumpyasnpa=np.array([......
  • 详解逻辑回归与评分卡-用逻辑回归制作评分卡-重复值和缺失值处理【菜菜的sklearn课堂
    视频作者:菜菜TsaiTsai链接:【技术干货】菜菜的机器学习sklearn【全85集】Python进阶_哔哩哔哩_bilibili在银行借贷场景中,评分卡是一种以分数形式来衡量一个客户的信用风......