Android app启动流程:广播的发送broadcastIntentLocked(6)

  • A+
所属分类:AMS
final int broadcastIntentLocked(ProcessRecord callerApp,

String callerPackage, Intent intent, String resolvedType,

IIntentReceiver resultTo, int resultCode, String resultData,

Bundle resultExtras, String[] requiredPermissions, int appOp, Bundle bOptions,

boolean ordered, boolean sticky, int callingPid, int callingUid, int userId) {

if (intent.getComponent() == null) {

if (userId == UserHandle.USER_ALL && callingUid == Process.SHELL_UID) {

for (int i = 0; i < users.length; i++) {

if (mUserController.hasUserRestriction(

UserManager.DISALLOW_DEBUGGING_FEATURES, users[i])) {

continue;

}

 List<BroadcastFilter> registeredReceiversForUser =  mReceiverResolver.queryIntent(intent, resolvedType, false, users[i]);

if (registeredReceivers == null) {

registeredReceivers = registeredReceiversForUser;

} else if (registeredReceiversForUser != null) {

registeredReceivers.addAll(registeredReceiversForUser);

}

}

} else {

registeredReceivers = mReceiverResolver.queryIntent(intent,resolvedType, false, userId);

}

}

 

while (ir < NR) {

if (receivers == null) {

receivers = new ArrayList();

}

receivers.add(registeredReceivers.get(ir));

ir++;

}

 

       if ((receivers != null && receivers.size() > 0) || resultTo != null) {

BroadcastQueue queue = broadcastQueueForIntent(intent);

BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,

callerPackage, callingPid, callingUid, resolvedType,

requiredPermissions, appOp, brOptions, receivers, resultTo, resultCode,

resultData, resultExtras, ordered, sticky, false, userId);

r.enqueueTime = SystemClock.uptimeMillis();

boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r);

if (!replaced) {

                queue.enqueueOrderedBroadcastLocked(r);

                queue.scheduleBroadcastsLocked();

}

} else {

if (intent.getComponent() == null && intent.getPackage() == null

&& (intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {

addBroadcastStatLocked(intent.getAction(), callerPackage, 0, 0, 0);

}

}

 

return ActivityManager.BROADCAST_SUCCESS;

}

 

其实这个函数比较复杂的,我们删减来关键部分,看到黑体部分:

1、将intent所描述的发送的receiver在mReceiverResolver 去找到;

2、当然接受者不仅仅是一个,有可能是多个。然后通过broadcastQueueForIntent 去拿到这个 queue;

3、然后queue调用相应的函数处理。

 

Android app启动流程:调用startProcessLocked的几种情况(1)

Android app启动流程:startProcessLocked函数分析(2)

Android app启动流程:Process.start(3)

Android app启动流程:startservice(4)

Android app启动流程:broadcast广播的注册(5)

Android app启动流程:广播的发送broadcastIntentLocked(6)

Android app启动流程:广播的发送scheduleBroadcastsLocked(7)

Android app启动流程:广播的发送processNextBroadcast(8)

Android app启动流程:关于FocusedStack的研究(9)

Android app启动流程:startActivityMayWait分析(10)

Android app启动流程:startActivityLocked分析(11)

Android app启动流程:Ams对activitystack和task的管理(12)

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: