Android PendingIntent


PendingIntent

描述

A description of an Intent and target action to perform with it. Instances of this class are created with getActivity, getActivities, getBroadcast, and getService; the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

它由getActivity,getService,getBroadcast来创建对象。

用于一种特殊的异步处理机制。可归结为 “异步激发”,这种异步激发常常是要跨进程执行的。比如说 A进程作为发起段,它可以从系统获得一个PendingIntent,然后A进程可以将PendingIntent对象通过binder机制“传递“给B进程,再通过B进程在未来的某个合适时机,”回调“PendingIntent对象的send()动作,完成激发。

PendingIntent的生命周期不与主进程相关。外部程序只能调用上述三个组件。

getActivity

static fun getActivity(
    context: Context!, 
    requestCode: Int, 
    intent: Intent!, 
    flags: Int
): PendingIntent!

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.

获得一个用于启动特定Activity的PendingIntent。

三个返回需要用到的参数

  • context:上下文对象
  • requstCode:请求码,发件人的私人请求代码(当前未使用)
  • intent:请求意图。用于要指明要启动的类以及数据的传递
  • flags:这是一个关键的标志位

flags常量

FLAG_CANCEL_CURRENT

static val FLAG_CANCEL_CURRENT: Int

Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one. For use with getActivity, getBroadcast, and getService.

如果新请求的 PendingIntent 发现已经存在时,取消已存在的,用新的 PendingInent 替换

FLAG_IMMUTABLE

static val FLAG_IMMUTABLE: Int

Flag indicating that the created PendingIntent should be immutable. This means that the additional intent argument passed to the send methods to fill in unpopulated properties of this intent will be ignored.

表示这是一个不可变的 PendingIntent。

FLAG_NO_CREATE

static val FLAG_NO_CREATE: Int

Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it. For use with getActivity, getBroadcast, and getService.

如果新请求的 PendingIntent 发现已经存在时,忽视新请求的,继续使用已经存在的请求。较少使用。

FLAG_ONE_SHOT

static val FLAG_ONE_SHOT: Int

Flag indicating that this PendingIntent can be used only once. For use with getActivity, getBroadcast, and getService.

表示 PendingIntent 只能使用一次,如果已经使用过,那么 getxxx方法时会返回为NULL,也就是说同类通知只能使用一次,后续的通知单独后无法打开。

FLAG_UPDATE_CURRENT

static val FLAG_UPDATE_CURRENT: Int

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity, getBroadcast, and getService.

如果新的请求的PendingIntent 发现已经存在时,如果 Intent 有字段改变了,则更新已存在的 PendingIntent

引用:

PendingIntent

PendingIntent的基本理解

Android PendingIntent


文章作者: TheCara
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC 4.0 许可协议。转载请注明来源 TheCara !
 上一篇
关于链式调用 关于链式调用
什么是链式调用当调用的样式为这样时。 可以不受限制的通过”.”操作符调用方法。 a.b().c() 此方法起源于 java8 实现 每个方法返回对象本身即可 public class Book{ private String n
2021-03-19
下一篇 
Android-PackageManager Android-PackageManager
PackageManager Class for retrieving various kinds of information related to the application packages that are currently
2020-12-07
  目录