BroadcastReceiver:
Activity 通過(guò)注冊(cè)褐缠,在ContextImpl中調(diào)研AMS registerReceiver();
AMS registerReceiver()中柠衅;
1 檢查 :?
? 檢查注冊(cè)的app浊吏,是否還存在丢早。
if (callerApp.info.uid != SYSTEM_UID &&
? ? ? ? !callerApp.pkgList.containsKey(callerPackage) {
…..
}
申請(qǐng)注冊(cè)的參數(shù)是否來(lái)自應(yīng)用自身:
if (callerApp.info.uid != SYSTEM_UID &&
? ? ? ? !callerApp.pkgList.containsKey(callerPackage) &&
? ? ? ? !"android".equals(callerPackage)) {
? ? throw new SecurityException("Given caller package " + callerPackage
? ? ? ? ? ? + " is not running in process " + callerApp);
}
是否是 instantApp
final HashMap<IBinder, ReceiverList> mRegisteredReceivers = new HashMap<>();
receiver 放到mRegisteredReceivers 的hashMap中
receiver被封裝成dispatcher注冊(cè)到BroadcastQueue理郑,
BroadcastReceiver的注冊(cè)基本完畢彪腔。
在Activity sendBroadcast 發(fā)送廣播,最終在contextImpl 中調(diào)研AMS的方法集中處理:
AMS中處理broadcastIntentLocked()
在這個(gè)過(guò)程中做了如下判斷, 判斷是否是系統(tǒng)廣播挽荠,不是系統(tǒng)應(yīng)用無(wú)法發(fā)送系統(tǒng)廣播等等
最后:BroadcastQueue中 scheduleBroadcastsLocked 向handler 發(fā)BROADCAST_INTENT_MSG的消息克胳,processNextBroadcast 方法中
如果是動(dòng)態(tài)注冊(cè)的receiver,其實(shí)可以知道圈匆,運(yùn)行進(jìn)程是活的漠另,接收對(duì)象是存在的,deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false)跃赚,
如果是靜態(tài)注冊(cè)的receiver笆搓,運(yùn)行進(jìn)程不確定是否存活,接收對(duì)象不存在纬傲,如果進(jìn)程未拉起满败,mService.startProcessLocked(),啟動(dòng)接收進(jìn)程然后繼續(xù)scheduleBroadcastsLocked()循環(huán)叹括,下次進(jìn)入processCurBroadcastLocked(r, app)算墨。既如果目標(biāo)進(jìn)程未啟動(dòng),這里是會(huì)拉起來(lái)的汁雷。如果進(jìn)程已啟動(dòng)净嘀,則processCurBroadcastLocked(r, app)分發(fā)廣播。
private void deliverToRegisteredReceiverLocked(BroadcastRecord r,
? ? ? ? BroadcastFilter filter, boolean ordered, int index){
? ? boolean skip = false;
? ? if (filter.requiredPermission != null) {
? ? ? ? int perm = mService.checkComponentPermission(filter.requiredPermission,
? ? ? ? ? ? ? ? r.callingPid, r.callingUid, -1, true);
? ? ? ? if (perm != PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? Slog.w(TAG, "Permission Denial: broadcasting "
? ? ? ? ? ? ? ? ? ? + r.intent.toString()
? ? ? ? ? ? ? ? ? ? + " from " + r.callerPackage + " (pid="
? ? ? ? ? ? ? ? ? ? + r.callingPid + ", uid=" + r.callingUid + ")"
? ? ? ? ? ? ? ? ? ? + " requires " + filter.requiredPermission
? ? ? ? ? ? ? ? ? ? + " due to registered receiver " + filter);
? ? ? ? ? ? skip = true;
? ? ? ? } else {
? ? ? ? ? ? final int opCode = AppOpsManager.permissionToOpCode(filter.requiredPermission);
? ? ? ? ? ? if (opCode != AppOpsManager.OP_NONE
&& mService.mAppOpsService.noteOperation(opCode, r.callingUid,
? ? ? ? ? ? ? ? ? ? ? ? ? ? r.callerPackage) != AppOpsManager.MODE_ALLOWED) {
? ? ? ? ? ? ? ? Slog.w(TAG, "Appop Denial: broadcasting "
? ? ? ? ? ? ? ? ? ? ? ? + r.intent.toString()
? ? ? ? ? ? ? ? ? ? ? ? + " from " + r.callerPackage + " (pid="
? ? ? ? ? ? ? ? ? ? ? ? + r.callingPid + ", uid=" + r.callingUid + ")"
? ? ? ? ? ? ? ? ? ? ? ? + " requires appop " + AppOpsManager.permissionToOp(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? filter.requiredPermission)
? ? ? ? ? ? ? ? ? ? ? ? + " due to registered receiver " + filter);
? ? ? ? ? ? ? ? skip = true;
? ? ? ? ? ? }
}
}
? ? if (!skip && r.requiredPermissions != null && r.requiredPermissions.length > 0) {
? ? ? ? for (int i = 0; i < r.requiredPermissions.length; i++) {
? ? ? ? ? ? String requiredPermission = r.requiredPermissions[i];
? ? ? ? ? ? int perm = mService.checkComponentPermission(requiredPermission,
? ? ? ? ? ? ? ? ? ? filter.receiverList.pid, filter.receiverList.uid, -1, true);
? ? ? ? ? ? if (perm != PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? ? ? Slog.w(TAG, "Permission Denial: receiving "
? ? ? ? ? ? ? ? ? ? ? ? + r.intent.toString()
? ? ? ? ? ? ? ? ? ? ? ? + " to " + filter.receiverList.app
+ " (pid=" + filter.receiverList.pid
+ ", uid=" + filter.receiverList.uid+ ")"
? ? ? ? ? ? ? ? ? ? ? ? + " requires " + requiredPermission
? ? ? ? ? ? ? ? ? ? ? ? + " due to sender " + r.callerPackage
? ? ? ? ? ? ? ? ? ? ? ? + " (uid " + r.callingUid + ")");
? ? ? ? ? ? ? ? skip = true;
break;
? ? ? ? ? ? }
? ? ? ? ? ? int appOp = AppOpsManager.permissionToOpCode(requiredPermission);
? ? ? ? ? ? if (appOp != AppOpsManager.OP_NONE&& appOp != r.appOp
? ? ? ? ? ? ? ? ? ? && mService.mAppOpsService.noteOperation(appOp,
? ? ? ? ? ? ? ? ? ? filter.receiverList.uid, filter.packageName)
? ? ? ? ? ? ? ? ? ? != AppOpsManager.MODE_ALLOWED) {
? ? ? ? ? ? ? ? Slog.w(TAG, "Appop Denial: receiving "
? ? ? ? ? ? ? ? ? ? ? ? + r.intent.toString()
? ? ? ? ? ? ? ? ? ? ? ? + " to " + filter.receiverList.app
+ " (pid=" + filter.receiverList.pid
+ ", uid=" + filter.receiverList.uid+ ")"
? ? ? ? ? ? ? ? ? ? ? ? + " requires appop " + AppOpsManager.permissionToOp(
? ? ? ? ? ? ? ? ? ? ? ? requiredPermission)
? ? ? ? ? ? ? ? ? ? ? ? + " due to sender " + r.callerPackage
? ? ? ? ? ? ? ? ? ? ? ? + " (uid " + r.callingUid + ")");
? ? ? ? ? ? ? ? skip = true;
break;
? ? ? ? ? ? }
}
}
? ? if (!skip && (r.requiredPermissions == null || r.requiredPermissions.length == 0)) {
? ? ? ? int perm = mService.checkComponentPermission(null,
? ? ? ? ? ? ? ? filter.receiverList.pid, filter.receiverList.uid, -1, true);
? ? ? ? if (perm != PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? Slog.w(TAG, "Permission Denial: security check failed when receiving "
? ? ? ? ? ? ? ? ? ? + r.intent.toString()
? ? ? ? ? ? ? ? ? ? + " to " + filter.receiverList.app
+ " (pid=" + filter.receiverList.pid
+ ", uid=" + filter.receiverList.uid+ ")"
? ? ? ? ? ? ? ? ? ? + " due to sender " + r.callerPackage
? ? ? ? ? ? ? ? ? ? + " (uid " + r.callingUid + ")");
? ? ? ? ? ? skip = true;
? ? ? ? }
}
? ? if (!skip && r.appOp != AppOpsManager.OP_NONE
&& mService.mAppOpsService.noteOperation(r.appOp,
? ? ? ? ? ? filter.receiverList.uid, filter.packageName)
? ? ? ? ? ? != AppOpsManager.MODE_ALLOWED) {
? ? ? ? Slog.w(TAG, "Appop Denial: receiving "
? ? ? ? ? ? ? ? + r.intent.toString()
? ? ? ? ? ? ? ? + " to " + filter.receiverList.app
+ " (pid=" + filter.receiverList.pid
+ ", uid=" + filter.receiverList.uid+ ")"
? ? ? ? ? ? ? ? + " requires appop " + AppOpsManager.opToName(r.appOp)
? ? ? ? ? ? ? ? + " due to sender " + r.callerPackage
? ? ? ? ? ? ? ? + " (uid " + r.callingUid + ")");
? ? ? ? skip = true;
? ? }
? ? if (!mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,
? ? ? ? ? ? r.callingPid, r.resolvedType, filter.receiverList.uid)) {
? ? ? ? skip = true;
? ? }
? ? if (!skip && (filter.receiverList.app== null || filter.receiverList.app.killed
|| filter.receiverList.app.crashing)) {
? ? ? ? Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r
? ? ? ? ? ? ? ? + " to " + filter.receiverList + ": process gone or crashing");
? ? ? ? skip = true;
? ? }
? ? // Ensure that broadcasts are only sent to other Instant Apps if they are marked as
// visible to Instant Apps.
? ? final boolean visibleToInstantApps =
? ? ? ? ? ? (r.intent.getFlags() & Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) != 0;
? ? if (!skip && !visibleToInstantApps && filter.instantApp
? ? ? ? ? ? && filter.receiverList.uid!= r.callingUid) {
? ? ? ? Slog.w(TAG, "Instant App Denial: receiving "
? ? ? ? ? ? ? ? + r.intent.toString()
? ? ? ? ? ? ? ? + " to " + filter.receiverList.app
+ " (pid=" + filter.receiverList.pid
+ ", uid=" + filter.receiverList.uid+ ")"
? ? ? ? ? ? ? ? + " due to sender " + r.callerPackage
? ? ? ? ? ? ? ? + " (uid " + r.callingUid + ")"
? ? ? ? ? ? ? ? + " not specifying FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS");
? ? ? ? skip = true;
? ? }
? ? if (!skip && !filter.visibleToInstantApp && r.callerInstantApp
? ? ? ? ? ? && filter.receiverList.uid!= r.callingUid) {
? ? ? ? Slog.w(TAG, "Instant App Denial: receiving "
? ? ? ? ? ? ? ? + r.intent.toString()
? ? ? ? ? ? ? ? + " to " + filter.receiverList.app
+ " (pid=" + filter.receiverList.pid
+ ", uid=" + filter.receiverList.uid+ ")"
? ? ? ? ? ? ? ? + " requires receiver be visible to instant apps"
? ? ? ? ? ? ? ? + " due to sender " + r.callerPackage
? ? ? ? ? ? ? ? + " (uid " + r.callingUid + ")");
? ? ? ? skip = true;
? ? }
? ? if (skip) {
? ? ? ? r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
return;
? ? }
? ? // If permissions need a review before any of the app components can run, we drop
// the broadcast and if the calling app is in the foreground and the broadcast is
// explicit we launch the review UI passing it a pending intent to send the skipped
// broadcast.
? ? if (mService.mPermissionReviewRequired) {
? ? ? ? if (!requestStartTargetPermissionsReviewIfNeededLocked(r, filter.packageName,
? ? ? ? ? ? ? ? filter.owningUserId)) {
? ? ? ? ? ? r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;
return;
? ? ? ? }
}
? ? r.delivery[index] = BroadcastRecord.DELIVERY_DELIVERED;
? ? // If this is not being sent as an ordered broadcast, then we
// don't want to touch the fields that keep track of the current
// state of ordered broadcasts.
? ? if (ordered) {
? ? ? ? r.receiver = filter.receiverList.receiver.asBinder();
? ? ? ? r.curFilter = filter;
? ? ? ? filter.receiverList.curBroadcast= r;
? ? ? ? r.state = BroadcastRecord.CALL_IN_RECEIVE;
? ? ? ? if (filter.receiverList.app!= null) {
? ? ? ? ? ? // Bump hosting application to no longer be in background
// scheduling class.? Note that we can't do that if there
// isn't an app...? but we can only be in that case for
// things that directly call the IActivityManager API, which
// are already core system stuff so don't matter for this.
? ? ? ? ? ? r.curApp = filter.receiverList.app;
? ? ? ? ? ? filter.receiverList.app.curReceivers.add(r);
? ? ? ? ? ? mService.updateOomAdjLocked(r.curApp, true);
? ? ? ? }
}
? ? try {
? ? ? ? if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
? ? ? ? ? ? ? ? "Delivering to " + filter + " : " + r);
? ? ? ? if (filter.receiverList.app!= null && filter.receiverList.app.inFullBackup) {
? ? ? ? ? ? // Skip delivery if full backup in progress
// If it's an ordered broadcast, we need to continue to the next receiver.
? ? ? ? ? ? if (ordered) {
? ? ? ? ? ? ? ? skipReceiverLocked(r);
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
? ? ? ? ? ? ? ? ? ? new Intent(r.intent), r.resultCode, r.resultData,
? ? ? ? ? ? ? ? ? ? r.resultExtras, r.ordered, r.initialSticky, r.userId);
? ? ? ? }
? ? ? ? if (ordered) {
? ? ? ? ? ? r.state = BroadcastRecord.CALL_DONE_RECEIVE;
? ? ? ? }
? ? } catch (RemoteException e) {
? ? ? ? Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
? ? ? ? if (ordered) {
? ? ? ? ? ? r.receiver = null;
? ? ? ? ? ? r.curFilter = null;
? ? ? ? ? ? filter.receiverList.curBroadcast= null;
? ? ? ? ? ? if (filter.receiverList.app!= null) {
? ? ? ? ? ? ? ? filter.receiverList.app.curReceivers.remove(r);
? ? ? ? ? ? }
}
}
}