PendingIntent详解
PendingIntent描述了一个Intent和目标action。这个类的实例是用getActivity(Context,int,Intent,int)方法和getBroadcast(Context,int,Intent,int)和getService(Context,int,Intent,int)方法创建的。创建出的pending
intent可以交给其它程序,于是它们可以在以后的某个时间以你的名义执行intent中所描述的动作。
通过给于一个PendingIntent,你可以使其它程序像你自己一样执行你所指定的操作(具有相同的权限和身份)。所以,你需要小心的创建PendingIntent:通常,你最基本的应该明确设置你的相关组件的名字,以保证将来intent是被发送给它而不是其它地方。
一个PendingIntent本是只是简单地引用一个由系统维护的一个令牌,这个令牌描述了用于取得PendingIntent的原始数据。这表示即使拥有这个PendingIntent的进程关闭了,但这个PendingIntent对于那些收到它的进程依然有效。如果创建这个PendingIntent的程序又重新运行并重新获取同一个类型的PendingIntent(相同的操作,action,数据,类型和组件以及相同的标志),那么获取的还是这个PendingIntent,并且这个程序可以用cancel()来删除这个PendingIntent。
Intent和PendingIntent的区别?
intent英文意思是意图,pending表示即将发生或来临的事情。
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。
Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中
保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a
PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。
本文翻译整理自:
public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
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)
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.
public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)
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.
public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
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.
public void send ()
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)
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)
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)
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. |
---|
public void cancel ()
Cancel a currently active PendingIntent. Only the original application owning an PendingIntent can cancel it.
只有PengdingIntent的原应用程序才能调用cancel()来把它从系统中移除掉。
getTargetPackage()函数
public String getTargetPackage ()
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.
PendingIntent详解相关推荐
- 关于Android Service真正的完全详解,你需要知道的一切
转载请注明出处(万分感谢!): http://blog.csdn.net/javazejian/article/details/52709857 出自[zejian的博客] Service全部 ...
- 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高...
第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...
- Android USB 开发详解
Android USB 开发详解 先附上 Android USB 官方文档 Android通过两种模式支持各种 USB 外设和 Android USB 附件(实现Android附件协议的硬件):USB ...
- Notification使用详解之三:通过服务更新进度通知在Activity中监听服务进度
为什么80%的码农都做不了架构师?>>> 上次我们讲到如何实现一个可更新的进度通知,实现的方式是启动一个线程模拟一个下载任务,然后根据任务进度向UI线程消息队列发送进度消息,U ...
- android系统(63)---Jobscheduler运行机制详解
android之Jobscheduler运行机制详解 如果想在将来达到一定条件下执行某项任务时,可以在一个实现了JobService的子类的onStartJob方法中执行这项任务,使用JobInfo的 ...
- Android Notification通知详解
Android Notification通知详解 Notification: (一).简介: 显示在手机状态栏的通知.Notification所代表的是一种具有全局效果的通知,程序一般通过Notifi ...
- Notification使用详解之四:由后台服务向Activity发送进度信息
上次讲到了如何在Activity中监听后台服务的进度信息,实现的方式是让Activity与后台服务绑定,通过中间对象Binder的实例操作后台服务.从效果上来讲,这种方式是可行的,不过这种实现有个缺点 ...
- Android 4.4 NotificationManagerService使用详解与原理分析(二)__原理分析
前置文章: <Android 4.4 KitKat NotificationManagerService使用详解与原理分析(一)__使用详解> 转载请务必注明出处:http://blog. ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
最新文章
- java maven -DskipTests 和 -Dmaven.test.skip=true 区别
- 快速排序python实现
- HashSet源码分析
- LeetCode动态规划 分割等和子集
- 实现小程序canvas拖拽功能
- 幸运数字(洛谷-P3292)
- python数据可视化代码_Python实现简单的数据可视化
- 威纶触摸屏与电脑连接_如何将威纶通tk6070ip触摸屏程序上传到电脑里。请大侠讲解具体步骤...
- 单总线结构CPU数据通路
- CentOS安装完没有ip地址的解决方法
- Android开发环境搭建(基于Android Studio)
- android打飞机游戏、MVP句子迷App、悬浮窗、RxJava+Retrofit、加载动画、定制计划App等源码
- php seekdir,perl 模式匹配总结和shell命令调用方法 (zz)
- pyTorch 图像分类模型训练教程
- mysql varchar 单引号_char、varchar数据类型值在使用时可以要用单引号或双引号括起来。...
- Draemon 360开源的基于Promtheus的升级版本告警系统
- Jetbrains系列产品重置试用方法
- php里opendir 返回值,php 目录遍历opendir函数
- 微信打飞机升级版(Qt实现)
- zeit_Zeit风格的Vue实现
热门文章
- 【C 语言】文件操作 ( 按照内存块的方式读写文件 | fread 函数 | fwrite 函数 )
- 【Java 并发编程】指令重排序规范 ( happens-before 先行发生原则 )
- 【Kotlin】Kotlin 单例 ( 懒汉式 与 恶汉式 | Java 单例 | Kotlin 单例 | 对象声明 | 伴生对象 | get 方法 | ? 与 !! 判空 )
- idea插件GsonFormat的使用
- ROS与Arduino学习(三)订阅与发布
- SVA Function Coverage
- 部署harbor1.2.0开启ldap验证
- JS-DOM Element方法和属性
- Net平台下的分布式缓存设计
- 多线程pthread_join()的作用