PendingIntent详解

【Unity】本地推送 Mobile Notifications Unity 官方本地Push解决方案,支持一次性或重复通知最新版本:2.1.1,支持Unity 2020.3及以上版本,支持Android5(API 21)和iOS 10.0+官方文档地址。 阅读详情

转自:http://hubingforever.blog.163.com/blog/static/17104057920117275222472/


本文翻译整理自:http://developer.android.com/reference/android/app/PendingIntent.html

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

Retrieve a PendingIntent that will start a new activity, like calling 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 launch flag in the Intent.

因为对于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 Private request code for the sender (currently not used).
intent Intent of the activity to be launched.
flags May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
当我们使用Intent.fillIn()方法时,表示其Intent的某个数据项可以被send方法的Inent参数进行覆盖重写。
Returns
  Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.
public static PendingIntent getActivities (Context context, int requestCode, Intent[] intents, int flags)
Since:  API Level 11

Like 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 launch flag in the Intent. (Activities after the first in the array are started in the context of the previous activity in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.)

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 togetActivity(Context, int, Intent, int), its content will be the subject of replacement by send(Context, int, Intent) andFLAG_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 May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.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
  • Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.
getService方法
该方法将系统取得一个 用于启动一个 Service 的PendingIntent对象.
public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)
Since:  API Level 1

Retrieve a PendingIntent that will start a service, like calling 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 May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.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
  • Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.
getBroadcast方法
该方法将从系统取 得一个用于向 BroadcastReceiver 的Intent广播的PendingIntent对象
public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
Since:  API Level 1

Retrieve a PendingIntent that will perform a broadcast, like calling 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 May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.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
  • Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.
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 Additional Intent data. See Intent.fillIn() for information on how this is applied to the original Intent.
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.

For the intent parameter, a PendingIntent often has restrictions on which fields can be supplied here, based on how the PendingIntent was retrieved ingetActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

Parameters
context The Context of the caller. This may be null if intent is also null.
该参数是因为intent参数才需要提供的,一般是当前的context,如果你的intent参数不为null的话,该函数也不能为null.
code Result code to supply back to the PendingIntent's target.
intent Additional Intent data. See Intent.fillIn() for information on how this is applied to the original Intent. Use null to not modify the original Intent.
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.

Android的PendingIntent简单示例,万字总结 PendingIntent.getService(Context, int, Intent, int),从系统取得一个用于启动一个Service的PendingIntent对象。 阅读详情

相关推荐

Intent和PendingIntent的区别

简单的总结了Intent和PendtingIntent的区别,经常与alermanger 和notificationmanager一起使用。

PendingIntent

PendingIntent可以看作是对Intent的一个封装,但它不是立刻执行某个行为, 而是满足某些条件或触发某些事件后才执行指定的行为。 PendingIntent的获取 PendingIntent获取有三种方式:通过Activity,Service,BroadcastReceiver获取. 1. 你可以通过getActivity(Context context, i...

weixin_30783913的博客 406

ROS2+MoveIt2+Gazebo仿真实战:从零搭建六轴机械臂控制闭环

本文详细介绍了如何从零搭建一个完整的ROS2+MoveIt2+Gazebo六轴机械臂仿真控制闭环。通过手写URDF模型、配置MoveIt2运动规划、集成Gazebo物理仿真与ROS2 Control控制器,最终实现RViz中规划轨迹、Gazebo中同步执行的完整数字孪生验证平台,为机器人算法开发和硬件验证提供低成本、高效的仿真解决方案。

weixin_29093017的博客 486

pendingIntent详解

pendingIntent字面意义:等待的,未决定的Intent。   pendingIntent对象,使用方法类的静态方法 :       getActivity(Context, int, Intent, int)------->跳转到一个activity组件、     getBroadcast(Context, int, Intent, int)------>打开一个广播组件    

一只攻城的柿子 734

redhawk-sc(seascape)学习日志2

design view是主view,是所有seascape view中最root的部分,一个design view可以驱动多种类型分析比如静态IR drop,动态IR drop以及EM.这些分析可以在不同的PVT和翻转率下.Design view是一个必须的view对于redhawk中的任何类型分析来说。seascape view相当于一个容器,用于保存各种和设计相关的数据,类似于逻辑信息、物理信息等等,每个view都有其对应的输入件和依赖,这些view都是redhawk用于各种类型的分析所需。

qq_39493558的博客 3497

【AI Infra】【RLHF框架】VeRL中WorkerGroup与Ray资源调度的协同机制剖析

本文深入剖析了VeRL框架中WorkerGroup与Ray资源调度的协同机制。通过解析Ray Placement Group的资源抽象和VeRL的RayResourcePool封装,详细阐述了WorkerGroup如何与底层资源精确绑定,并实现RLHF训练中异构、多阶段任务的灵活资源分配与动态调整,为大规模分布式强化学习训练提供了高效解决方案。

weixin_29204205的博客 391

Android PendingIntent详解

PendingIntent主要有下面几个静态方法(当然还有其它方法) PendingIntent.getActivity(Contextcontext, int requestCode,Intentintent, int flags) --->启动Activity PendingIntent.getBroadcast(Contextcontext, int...

GracefulGuigui的博客 1887

从波形图到参数调优:一次无感BLDC电机启动失败的深度解析

本文深入解析无感BLDC电机启动失败案例,通过波形分析揭示参数不匹配和IPM保护机制触发等关键问题。详细探讨了过零点检测、PWM周期设置及强拖参数调优的底层逻辑,并分享IPM模块保护机制与实战调优技巧,最终实现99.98%的启动成功率。

weixin_29182481的博客 820

[Android] PendingIntent详解

转:http://my.oschina.net/u/242041/blog/206710  Intent是一个意图,一个描述了想要启动一个Activity、Broadcast或是Service的意图。它主要持有的信息是它想要启动的组件(Activity、Broadcast或是Service),在开发操作中,需要通过 startActivity , startService 或send

zhangmiaoping23的专栏 1万+

飞牛fnNAS搭建Web网页版OFFICE(WPS)软件

本文介绍了如何在飞牛NAS上通过Docker容器部署WPS Office办公软件。主要内容包括:启用Docker服务、准备项目文件夹结构、使用Compose方式构建容器、解决端口冲突问题、配置中文输入法和字体库,以及通过ZeroTier实现远程访问。文章详细说明了每个步骤的操作方法,包括容器启动、输入法设置、字体添加和文件存储位置配置。最后还介绍了使用ZeroTier内网穿透技术实现远程访问NAS上WPS服务的方法,让用户可以在不同设备上随时编辑NAS中的文件。整个部署过程完整清晰,为NAS用户提供了便捷的

dgnankai的专栏 1701

网红商品数据抓取:基于Python的高效爬虫实战

本篇博客介绍了从爬取到存储、分析和可视化网红商品数据的完整流程。通过Python的强大生态,我们能够轻松完成复杂的数据抓取任务。在实际应用中,还可以将此爬虫与机器学习模型结合,进一步挖掘数据价值。

2201_76125261的博客 1476

译:PendingIntent详解

博客源址:译:PendingIntent详解 博客时间:2011-12-11 10:54 本文翻译整理自:http://developer.android.com/reference/android/app/PendingIntent.html public final class PendingIntent extends Object implements Pa

Android移动开发者的专栏 616

基于 Qt C++ 开发一套西门子量子测量系统的控制与数据采集平台

sensorIdCombo->addItems({"QTS-100-001 (温度)", "QPG-200-001 (压力)", "QDS-300-001 (位移)"});precisionTable->setHorizontalHeaderLabels({"传感器ID", "类型", "标称精度", "实际精度", "校准状态"});QWidget *dispWidget = createSensorWidget("量子位移", "mm (±1μm)", 50.0, 0.001);// 传感器状态更新。

寒寒豆豆软件公司的博客 254
上一篇: 小米4安卓开发真机调试在Eclipse中Unknown的解决方法
下一篇: Android Intent Action 大全
KingLearnJava
博客等级 码龄12年 93粉丝 84原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值