BroadcastReceiver原理

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);

? ? ? ? ? ? }

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末侠讯,一起剝皮案震驚了整個(gè)濱河市挖藏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌厢漩,老刑警劉巖膜眠,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡宵膨,警方通過(guò)查閱死者的電腦和手機(jī)架谎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)柄驻,“玉大人狐树,你說(shuō)我怎么就攤上這事焙压『枧В” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵涯曲,是天一觀的道長(zhǎng)野哭。 經(jīng)常有香客問(wèn)我,道長(zhǎng)幻件,這世上最難降的妖魔是什么拨黔? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮绰沥,結(jié)果婚禮上篱蝇,老公的妹妹穿的比我還像新娘。我一直安慰自己徽曲,他們只是感情好零截,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著秃臣,像睡著了一般涧衙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上奥此,一...
    開(kāi)封第一講書(shū)人閱讀 51,718評(píng)論 1 305
  • 那天弧哎,我揣著相機(jī)與錄音,去河邊找鬼稚虎。 笑死撤嫩,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蠢终。 我是一名探鬼主播非洲,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼蜕径!你這毒婦竟也來(lái)了两踏?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤兜喻,失蹤者是張志新(化名)和其女友劉穎梦染,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡帕识,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年泛粹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肮疗。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡晶姊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出伪货,到底是詐尸還是另有隱情们衙,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布碱呼,位于F島的核電站蒙挑,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏愚臀。R本人自食惡果不足惜忆蚀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望姑裂。 院中可真熱鬧馋袜,春花似錦、人聲如沸舶斧。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)捧毛。三九已至观堂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間呀忧,已是汗流浹背师痕。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留而账,地道東北人胰坟。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像泞辐,于是被迫代替她去往敵國(guó)和親笔横。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

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