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 您需要知道的那些事

相关内容

热门资讯

电视安卓系统哪个品牌好,哪家品... 你有没有想过,家里的电视是不是该升级换代了呢?现在市面上电视品牌琳琅满目,各种操作系统也是让人眼花缭...
安卓会员管理系统怎么用,提升服... 你有没有想过,手机里那些你爱不释手的APP,背后其实有个强大的会员管理系统在默默支持呢?没错,就是那...
安卓系统软件使用技巧,解锁软件... 你有没有发现,用安卓手机的时候,总有一些小技巧能让你玩得更溜?别小看了这些小细节,它们可是能让你的手...
安卓系统提示音替换 你知道吗?手机里那个时不时响起的提示音,有时候真的能让人心情大好,有时候又让人抓狂不已。今天,就让我...
安卓开机不了系统更新 手机突然开不了机,系统更新还卡在那里,这可真是让人头疼的问题啊!你是不是也遇到了这种情况?别急,今天...
安卓系统中微信视频,安卓系统下... 你有没有发现,现在用手机聊天,视频通话简直成了标配!尤其是咱们安卓系统的小伙伴们,微信视频功能更是用...
安卓系统是服务器,服务器端的智... 你知道吗?在科技的世界里,安卓系统可是个超级明星呢!它不仅仅是个手机操作系统,竟然还能成为服务器的得...
pc电脑安卓系统下载软件,轻松... 你有没有想过,你的PC电脑上安装了安卓系统,是不是瞬间觉得世界都大不一样了呢?没错,就是那种“一机在...
电影院购票系统安卓,便捷观影新... 你有没有想过,在繁忙的生活中,一部好电影就像是一剂强心针,能瞬间让你放松心情?而我今天要和你分享的,...
安卓系统可以写程序? 你有没有想过,安卓系统竟然也能写程序呢?没错,你没听错!这个我们日常使用的智能手机操作系统,竟然有着...
安卓系统架构书籍推荐,权威书籍... 你有没有想过,想要深入了解安卓系统架构,却不知道从何下手?别急,今天我就要给你推荐几本超级实用的书籍...
安卓系统看到的炸弹,技术解析与... 安卓系统看到的炸弹——揭秘手机中的隐形威胁在数字化时代,智能手机已经成为我们生活中不可或缺的一部分。...
鸿蒙系统有安卓文件,畅享多平台... 你知道吗?最近在科技圈里,有个大新闻可是闹得沸沸扬扬的,那就是鸿蒙系统竟然有了安卓文件!是不是觉得有...
宝马安卓车机系统切换,驾驭未来... 你有没有发现,现在的汽车越来越智能了?尤其是那些豪华品牌,比如宝马,它们的内饰里那个大屏幕,简直就像...
p30退回安卓系统 你有没有听说最近P30的用户们都在忙活一件大事?没错,就是他们的手机要退回安卓系统啦!这可不是一个简...
oppoa57安卓原生系统,原... 你有没有发现,最近OPPO A57这款手机在安卓原生系统上的表现真是让人眼前一亮呢?今天,就让我带你...
安卓系统输入法联想,安卓系统输... 你有没有发现,手机上的输入法真的是个神奇的小助手呢?尤其是安卓系统的输入法,简直就是智能生活的点睛之...
怎么进入安卓刷机系统,安卓刷机... 亲爱的手机控们,你是否曾对安卓手机的刷机系统充满好奇?想要解锁手机潜能,体验全新的系统魅力?别急,今...
安卓系统程序有病毒 你知道吗?在这个数字化时代,手机已经成了我们生活中不可或缺的好伙伴。但是,你知道吗?即使是安卓系统,...
奥迪中控安卓系统下载,畅享智能... 你有没有发现,现在汽车的中控系统越来越智能了?尤其是奥迪这种豪华品牌,他们的中控系统简直就是科技与艺...