PendingIntent

创建

调用封装好的接口创建PI

PendingIntent pi = PendingIntent.getActivity(mContext, 0, new Intent("com.a.b"), 0);

Notification n = new Notification.Builder()
	.setContentIntent(pi)
	..
	.build();

原理

总结

  1. app 端会调用AMS 获取 PIRecord
  2. AMS 创建PIRecord 后,会自己保存一份,再返回给app

app中调用AMS获取PendingIntentRecord,封装成PendingIntent

class PendingIntent {
	static PendingIntent getActivity(.., Intent intent, .) {
		...
		
		try {
			..
			
			// 调用AMS,获取PIRecord
			IIntentSender target = 
				ActivityManger.getService().getIntentSender(
						ActivityManager.INTENT_SENDER_ACTIVITY,
						...,
						new Intent[] {intent},
						...
						); 
			// 封装 PIRecord。
			return target != null ? new PendingIntent(target) : null;
		}
	}
}

AMS 创建PendingIntentRecord并返回

描述
  1. AMS 创建PIRecord,intent的数据保存在 PIRecord.key里
  2. AMS还会保存这个Record 在 AMS.mPendingIntentController.mIntentSenderRecords 里
  3. 把Record返回给app
代码
class AMS {
	IIntentSender getIntentSender(..., Intent[] intents, ...) {
		...
		
		// 创建 PendingIntentRecord并返回。并保存到 PendingIntentController.mIntentSenderRecords 里。
		// intents会保存到 PIRecord.key 里
		return mPendingIntentController.getIntentSender(..., intents, ...);
	}
}

发送PendingIntent

封装接口的使用

pendingIntent.send();

原理

app端

描述

把PendingIntentRecord 传给AMS,去发送这个Intent

代码
class PendingIntent {
	void send() {
		send(...);
	}
	void send(...) {
		if (sendAndReturnResult(...) < 0) { // 在if里执行的
		}
	}

	int sendAndReturnResult(...) {
		try {
			.
			return ActivityManager.getService().sendIntentSender(
				mTarget, // mTarget 这个是AMS返回来的Binder:PendingIntentRecord
				...
				);
		}
	}
}

AMS端

描述

把创建PI时的Intent,传给ATMS继续处理

代码

class AMS {
	int sendIntentSender(IIntentSender target, ...) {
		...
		
		if (target instanceof PendingIntentRecord) {
			return target.sendWithResult(...);
		}
	}
}

我们创建PendingIntent时,就传了 intent,这个intent被保存在 PIRecord.key.allIntents 和 PIRecord.key.requestIntent 里

class PIRecord {
	int sendWithResult(...) {
		return sendInner(...);
	}

	int sendInner(...) {
		
		...
		
		// 这个就是创建 PendingIntent 时的 Intent
		finalIntent = key.requestIntent != null ? new Intent(key.requestIntent) : new Intent();
		
		...
		
		try {
			...
			switch (key.type) {
				case ActivityManager.intent_sender_activity:
					try {
						if (key.allIntents.length > 1) {
						} else {
							res = controller.mAtmInternal.startActivityInPackage(
								...,
								finalIntent,
								...
							)
						}
					}
					break;
			}
		}
	}
}

ATMS端

描述

用ActivityStart 来发送Intent,就像Context.startActivity() 了

代码

class ATMS {
	class LocalService {
		int startActivityInPackage(..., Intent intent, ...) {
			..
			return getActivityStartController().startActivityInPackge(..., intent, ...);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PendingIntent.getBroadcast()是Android中的一个方法,用于创建一个用于发送广播的PendingIntent对象。广播是一种用于在Android应用程序之间传递消息或事件的机制。 使用PendingIntent.getBroadcast()方法,可以创建一个待处理的意图(Intent),当该意图被触发时,系统将发送一个广播。这个广播可以被其他应用程序接收并做出相应的处理。 该方法的语法如下: ```java public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) ``` 参数说明: - context:上下文对象,一般为Activity或Service的实例; - requestCode:请求码,用于标识PendingIntent的唯一性; - intent:要发送的广播意图; - flags:标志位,用于设置PendingIntent的行为。 示例代码: ```java Intent broadcastIntent = new Intent(context, MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); ``` 其中,MyBroadcastReceiver是自定义的广播接收器类,用于接收并处理广播消息。requestCode参数可根据需要进行设置,用于区分不同的PendingIntent对象。 注意:使用PendingIntent.getBroadcast()方法创建的PendingIntent对象只能用于发送广播,而不能用于启动Activity或Service。如果需要启动Activity或Service,需要使用PendingIntent.getActivity()或PendingIntent.getService()方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值