PendingIntent
admin
2024-01-21 04:50:16
0

PendingIntent 可以看作是对 Intent 的一个封装,但它不是立即执行某个行为,而是满足某些条件或者触发某些事件后才执行指定的行为。

获取 PendingIntent 的方法有以下三种,分别是通过 Activity、Service、BroadcastReceiver 获取:

  • 通过 getActivity 系列方法从系统中获取一个用于启动 Activity 的 PendingIntent 对象;
  • 通过 getService 系列方法从系统中获取一个用于启动 Service 的 PendingIntent 对象;
  • 通过 getBroadcast 系列方法从系统中获取一个用于启动 BroadcastReceiver 的 PendingIntent 对象;

以下是相关源码:

public final class PendingIntent implements Parcelable {public static PendingIntent getActivity(Context context, int requestCode,Intent intent, @Flags int flags) {return getActivity(context, requestCode, intent, flags, null);}public static PendingIntent getActivity(Context context, int requestCode,@NonNull Intent intent, @Flags int flags, @Nullable Bundle options) {String packageName = context.getPackageName();String resolvedType = intent != null ? intent.resolveTypeIfNeeded(context.getContentResolver()) : null;try {intent.migrateExtraStreamToClipData(context);intent.prepareToLeaveProcess(context);IIntentSender target =ActivityManager.getService().getIntentSenderWithFeature(ActivityManager.INTENT_SENDER_ACTIVITY, packageName,context.getAttributionTag(), null, null, requestCode, new Intent[] { intent },resolvedType != null ? new String[] { resolvedType } : null,flags, options, context.getUserId());return target != null ? new PendingIntent(target) : null;} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}public static PendingIntent getActivities(Context context, int requestCode,@NonNull Intent[] intents, @Flags int flags) {return getActivities(context, requestCode, intents, flags, null);}public static PendingIntent getActivities(Context context, int requestCode,@NonNull Intent[] intents, @Flags int flags, @Nullable Bundle options) {String packageName = context.getPackageName();String[] resolvedTypes = new String[intents.length];for (int i=0; iintents[i].migrateExtraStreamToClipData(context);intents[i].prepareToLeaveProcess(context);resolvedTypes[i] = intents[i].resolveTypeIfNeeded(context.getContentResolver());}try {IIntentSender target =ActivityManager.getService().getIntentSenderWithFeature(ActivityManager.INTENT_SENDER_ACTIVITY, packageName,context.getAttributionTag(), null, null, requestCode, intents, resolvedTypes,flags, options, context.getUserId());return target != null ? new PendingIntent(target) : null;} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}public static PendingIntent getService(Context context, int requestCode,@NonNull Intent intent, @Flags int flags) {return buildServicePendingIntent(context, requestCode, intent, flags,ActivityManager.INTENT_SENDER_SERVICE);}public static PendingIntent getBroadcast(Context context, int requestCode,Intent intent, @Flags int flags) {return getBroadcastAsUser(context, requestCode, intent, flags, context.getUser());}
}

对于参数 flag 可以有以下状态:

  • FLAG_NO_CREATE:如果当前系统中已经存在一个相匹配的 PendingIntent 对象,将不进行创建,如果不存在,也不会创建该 PendingIntent 对象,而是直接返回 NULL;
  • FLAG_ONE_SHOT:该 PendingIntent 只作用一次,在该 PendingIntent 对象通过 send 方法触发过后,PendingIntent 将自动调用 cancel 进行销毁。如果再次调用 send 方法的话,系统将会返回一个 SendIntentException
  • FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相匹配的 PendingIntent,那么,将已有的 PendingIntent 取消,再重新生成一个 PendingIntent 对象;
  • FLAG_UPDATE_CURRENT:如果系统中有一个相匹配的 PendingIntent,系统将使用该 PendingIntent 对象,但是,会使用新的 Intent.Extra 来更新之前的 PendingIntent 中的 Intent 数据;

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

PendingIntent 的匹配规则:如果两个 PendingIntent 的 Intent 相同并且 requestCode 也相同,那么这两个 PendingIntent 是相匹配的。

Intent 的匹配规则:如果两个 Intent 的 ComponentName 和 intent-filter 都相同,那么这两个 Intent 是相同的,Extras 不参加 Intent 的匹配过程。以下是 Intent.filterEquals 方法:

public class Intent implements Parcelable, Cloneable {/* * Determine if two intents are the same for the purposes of intent resolution (filtering). Thatis,  * if their action, data, type, identity, class, and categories are the same. This does not compare * any extra data included in the intents. Note that technically when actually matching against an * IntentFilter the identifier is ignored, while here it is directly compared for equality like the * other fields.* Params:* other – The other Intent to compare against.* Returns:* Returns true if action, data, type, class, and categories are the same.*/public boolean filterEquals(Intent other) {if (other == null) {return false;}if (!Objects.equals(this.mAction, other.mAction)) return false;if (!Objects.equals(this.mData, other.mData)) return false;if (!Objects.equals(this.mType, other.mType)) return false;if (!Objects.equals(this.mIdentifier, other.mIdentifier)) return false;if (!(this.hasPackageEquivalentComponent() && other.hasPackageEquivalentComponent())&& !Objects.equals(this.mPackage, other.mPackage)) {return false;}if (!Objects.equals(this.mComponent, other.mComponent)) return false;if (!Objects.equals(this.mCategories, other.mCategories)) return false;return true;}
}

如果是 Android 12 及以上的系统,要求为每个创建的 PendingIntent 对象指定可变性,这样做可以提高应用的安全性。如需声明 PendingIntent 对象是否可变,分别使用 PendingIntent.FLAG_MUTABLE 或 PendingIntent.FLAG_IMMUTABLE 标志。如果在不设置任何可变标志的情况下创建 PendingIntent 对象,系统会抛出 IllegalArgumentException。

在 Android 12 之前的版本中,不带有 FLAG_IMMUTABLE 标记创建的 PendingIntent 默认是可变类型的。

PendingIntent 的使用场景:

  • 通知,在点击通知时执行调起本应用的操作或者其他操作;
  • 闹钟,定时执行某个操作;
  • 桌面小部件,点击小部件时执行某个操作;

参考

Android基础——PendingIntent理解
PendingIntent详解
PendingIntent
PendingIntent的解惑
关于 PendingIntent 您需要知道的那些事

相关内容

热门资讯

安卓系统不推送更新,揭秘背后的... 最近是不是发现你的安卓手机有点儿“懒”啊?更新推送总是慢吞吞的,让人等得花儿都谢了。别急,今天就来给...
ape格式转换安卓系统,享受音... 你有没有想过,你的安卓手机里的ape格式音乐文件,竟然可以通过一个小小的转换,焕发出全新的生命力?没...
获取安卓系统加载器,核心功能与... 你有没有想过,你的安卓手机里那些神奇的软件和游戏是怎么被安装到你的设备上的呢?没错,就是通过一个叫做...
安卓系统文件夹在哪,安卓系统文... 你有没有遇到过这样的情况:手机里乱糟糟的,想找个文件却找不到?别急,今天就来给你揭秘安卓系统文件夹的...
安卓手感最好的裸机系统,安卓手... 安卓手感最好的裸机系统:探索极致体验的秘密武器在数字世界中,我们常常被各种功能和复杂操作所包围,尤其...
nas如何刷回安卓系统,轻松刷... 你有没有想过,你的NAS(网络附加存储)突然间变成了一个安卓的小天地?别急,这可不是什么天方夜谭,而...
荣耀沿用的安卓系统吗,打造个性... 你有没有注意到,最近荣耀的新机发布,大家都在热议一个问题:荣耀沿用的安卓系统吗?这可是个让人好奇不已...
快麦erp系统安卓下载,一键下... 你有没有听说最近一款叫做快麦ERP系统的软件在安卓平台上大受欢迎呢?没错,就是那个能让你企业管理如虎...
华为安卓系统下载app,一步到... 你有没有发现,最近华为手机的用户们都在忙活一件大事儿?没错,那就是下载安卓系统上的各种app啦!这可...
原生安卓系统游戏模式,畅享沉浸... 亲爱的手机游戏爱好者们,你是否曾为手机游戏运行不畅而烦恼?又或者,你是否渴望在游戏中获得更极致的体验...
安卓9改系统语言设置,轻松切换... 你有没有发现,手机里的语言设置有时候真的让人头疼?比如说,你突然想用一下安卓9的系统语言设置,结果发...
怎么升级安卓最新系统,畅享安卓... 亲爱的手机控们,你是不是也和我一样,对安卓系统的更新充满了期待?每次系统升级,都仿佛给我们的手机带来...
安卓系统电视跳舞毯,家庭娱乐新... 你有没有想过,家里的电视除了用来追剧、看电影,还能变成一个充满活力的娱乐中心?没错,我要给你介绍的就...
安卓系统维护周期,全方位守护您... 亲爱的手机控们,你是不是也和我一样,对安卓系统的维护周期充满了好奇呢?毕竟,我们的手机可是我们日常生...
安卓系统电脑怎么往下滑,一扫即... 你有没有发现,用安卓系统电脑的时候,有时候屏幕上会出现一些小图标或者应用,你想要快速浏览或者切换,却...
手机中判断安卓系统苹果系统js... 你有没有想过,你的手机里到底装的是安卓系统还是苹果系统呢?这可不是一个小问题哦,因为不同的系统,就像...
window系统和安卓系统还原... 你有没有遇到过手机或电脑突然卡顿,或者不小心删掉了重要的文件?别急,今天就来给你详细说说如何让win...
安卓系统打电话变声器,轻松实现... 安卓系统打电话变声器:探索数字时代的通信革新在数字化浪潮中,智能手机已经成为我们生活中不可或缺的一部...
android系统和安卓哪个好... 说到手机操作系统,你是不是也和我一样,对Android系统和安卓系统傻傻分不清楚呢?别急,今天就来给...
米柚系统是不是安卓,基于安卓的... 亲爱的读者,你是否曾在手机的选择上犹豫不决,尤其是当面对那些自称是安卓系统但又有自己特色的操作系统时...