BroadcastReceiver原理

BroadcastReceiver:

Activity 通過注冊(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ù)是否來自應(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è)過程中做了如下判斷, 判斷是否是系統(tǒng)廣播,不是系統(tǒng)應(yīng)用無法發(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ì)拉起來的。如果進(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);

? ? ? ? ? ? }

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、動(dòng)態(tài)注冊(cè)過程源碼分析: 在Activity中動(dòng)態(tài)注冊(cè)廣播室,在注冊(cè)方法之前其實(shí)省略了Context,也就是實(shí)際...
    騎著豬的蝸牛閱讀 856評(píng)論 0 1
  • 本文重點(diǎn)介紹應(yīng)用程序的啟動(dòng)過程,應(yīng)用程序的啟動(dòng)過程實(shí)際上就是應(yīng)用程序中的默認(rèn)Activity的啟動(dòng)過程,本文將詳細(xì)...
    天宇sonny閱讀 470評(píng)論 1 0
  • # 基于8.0源碼解析:startService 啟動(dòng)過程歡迎關(guān)注我的公眾號(hào): ![](http://opq81r...
    lantier閱讀 599評(píng)論 0 0
  • 1 進(jìn)程啟動(dòng)過程 Android應(yīng)用程序框架層創(chuàng)建的應(yīng)用程序進(jìn)程具有兩個(gè)特點(diǎn),一是進(jìn)程的入口函數(shù)是Activit...
    Kevin_Junbaozi閱讀 4,319評(píng)論 0 23
  • 1 Old problems exist, and new problems are beginning to e...
    瑩仔Skye閱讀 239評(píng)論 0 0

友情鏈接更多精彩內(nèi)容