目录
- 正文
- 广播的注册
- 广播的解注册
- 广播的发送
- 总结
正文
本文整体阅读下来相对Activity
和Service
的启动流程较容易,比较贴近我们日常代码开发习惯。我们曾经有个整机项目,多个APP跨进程交互,本来想采用AIDL进行的,但最终考虑到项目工期和其他同事的能力,最终在采用广播方式进行IPC。
那时,自己也在想,这么多个APP相互发信息,数据量也大,对整机性能有影响么?会不会存在丢失和内存问题。一脸茫然,网上也不会有类似信息告诉总结这种情况,本文也不会总结这个答案,因为看完之后心中自然有数了。
在AMS中持有集合用于存储所有的广播,应用程序可以从向其注册和解注册广播。当应用发送广播时,AMS检查相关权限和特殊的Intent
。然后再根据对应IntentFilter
匹配到一个或多个Receiver
,在应用进程回调其onReceive
函数。
阅读源码本身就是一份苦活,不可能一次就读懂,或者了解透的。只有反复的阅读,输入与输出,才会越来越轻松。所以个人建议,先粗读,了解个大概的思路就行。收藏或点赞,等自己ready,再好好结合源码阅读。一定要Fuck Code!
广播的注册
我们常在Activity
或Service
、甚至Application
中调用registerReceiver
函数来注册动态广播,该函数其实来自它们共同的父类ContextWrapper
中。ContextWrapper
是Context
的子类,我们会在介绍Context
的文章介绍它们的关系。
public Intent registerReceiver( BroadcastReceiver receiver, IntentFilter filter) { return mBase.registerReceiver(receiver, filter); }
这里Context
类型的mBase
,在Activity
的创建过程实际被赋值为ContextImpl
实例。
public Intent registerReceiver(BrojavascriptadcastReceiver receiver, IntentFilter filter) { return registerReceiver(receiver, filter, null, null); } public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler) { return registerReceiverInternal(receiver, getUserId(), filter, broadcastPermission, scheduler, getOuterContext(), 0); }
经过registerReceiver
重载函数,调用了registerReceiverInternal
函数。
private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId, IntentFilter filter, String broadcastPermission, Handler scheduler, Context context, int flags) { IIntentReceiver rd = null; //分析一 if (receiver != null) { if (mPackageInfo != null && context != null) { if (scheduler == null) { scheduler = mMainThread.getHandler(); } rd = mPackageInfo.getReceiverDispatcher( receiver, context, scheduler, mMainThread.getInstrumentation(), true); } else { if (scheduler == null) { scheduler = mMainThread.getHandler(); } rd = new LoadedApk.ReceiverDispatcher( receiver, context, scheduler, null, true).getIIntentReceiver(); } } try { 分析二: final Intent intent = ActivityManager.getService().registerReceiverWithFeature( mMainThread.getApplicationThread(), mBasePackageName, getAttributionTag(), rd, filter, broadcastPermission, userId, flags); if (intent != null) { intent.setExtrasClassLoader(getClassLoader()); intent.prepareToEnterProcess(); } return intent; } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }
分析一:
传递进来的BroadcastReceiver
不为null
,LoadedApk
类型的mPackageInfo
只要应用进程启动,该属性就会被赋值,context
这里指向Activity
。scheduler
为null
,赋值为主线程的H
类型mH
对象。分析一,主要通过上面的变量来获得IIntentReceiver
类型rd
对象。
getReceiverDispatcher
函数先从缓存检测是否有相同类型的BroadcastReceiver
对应的ReceiverDispatcher
。没有的话,则新建并缓存起来。 一个context
对应多个BroadcastReceiver
,而一个BroadcastReceiver
对应用一个ReceiverDispatcher
。
ReceiverDispatcher
是LoadedDispatcher
的静态内部类,其内部还有一个AIDL
类型本地实现静态类InnerReceiver
。在ReceiverDispatcher
的构造函数中会创建InnerReceiver
的实例。
public IIntentReceiver getReceiverDispatcher(BroadcastReceiver r, Context context, Handler handler, Instrumentation instrumentation, boolean registered) { synchronized (mReceivers) { LoadedApk.ReceiverDispatcher rd = null; ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> map = null; if (registered) { map = mReceivers.get(context); if (map != null) { rd = map.get(r); } } if (rd == null) { rd = new ReceiverDispatcher(r, context, handler, instrumentation, registered); if (registered) { if (map == null) { map = new ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>(); mReceivers.put(context, map); } map.put(r, rd); } } else { rd.validate(context, handler); } rd.mForgotten = false; return rd.getIIntentReceiver(); } }
回到registerReceiverInternal
函数的分析二,调用了AMS
的registerReceiverWithFeature
函数。
该函数是Broadcast
整个注册过程结束的地方,根据新注册的BroadcastReceiver
,处理粘性广播的发送和当前注册Receiver
的添加。
分析一:
粘性广播存储在AMS的SparseArray<ArrayMap<String, ArrayList<Intent>>>
类型的 mStickyBroadcasts
中。SparseArray
的key
为userId
,而ArrayMap
的key
为action
,value
为Intent
。即我们可以通过用户id在mStickyBroadcasts
找到当前进程对应所有粘性广播(和针对所有进程的粘性广播),然后根据对应的action
找到对应的Intent
。这里将他们收集到stickyIntents
集合中。
分析二:
所有广播的接收者BroacastReceiver
存储在AMS
类 HashMap<IBinder, ReceiverList>
类型的mRegisteredReceivers
中。这里的IBinder
类型就是应用进程前面创建的InnerReceiver
类实例在AMS的引用。因为广播接收者BroadcastReceiver
对应一个或多个Broadcast
,所以这里通过继承自ArrayList<BroadcastFilter>
的ReceiverList
来表达这种关系。通过BroadcastFilter
来表示当前接收者感兴趣的广播。
分析三:
对匹配到的粘性Intent进入广播队列广播。
public Intent registerReceiverWithFeature(IApplicationThread caller, String callerPackage, String callerFeatureId, IIntentReceiver receiver, IntentFilter filter, String permission, int userId, int flags) { enforceNotIsolatedCaller("registerReceiver"); //粘性Intent ArrayList<Intent> stickyIntents = null; ProcessRecord callerApp = null; final boolean visibleToInstantApps = (flags & Context.RECEIVER_VISIBLE_TO_INSTANT_APPS) != 0; int callingUid; int callingPid; boolean instantApp; synchronized(this) { if (caller != null) { //获得当前引用进程的ProcessRecord callerApp = getRecordForAppLocked(caller); ... callingUid = callerApp.info.uid; callingPid = callerApp.pid; } else { callerPackage = null; callingUid = Binder.getCallingUid(); callingPid = Binder.getCallingPid(); } //是否快应用(类似小程序) instantApp = isInstantApp(callerApp, callerPackage, callingUid); userId = mUserController.handleIncomingUser(callingPid, callingUid, userId, true, ALLOW_FULL_ONLY, "registerReceiver", callerPackage); //分析一:当前注册广播中感兴趣的action列表 Iterator<String> actions = filter.actionsIterator(); if (actions == null) { ArrayList<String> noAction = new ArrayList<String>(1); noAction.add(null); actions = noAction.iterator(); } //从历史粘性广播中查找与当前注册的action一致的intent //添加到stickyIntents // Collect stickies of users int[] userIds = { UserHandle.USER_ALL, UserHandle.getUserId(callingUid) }; while (actions.hasNext()) { String action = actions.next(); for (int id : userIds) { ArrayMap<String, ArrayList<Intent>> stickies = mStickyBroadcasts.get(id); if (stickies != null) { ArrayList<Intent> intents = stickies.get(action); if (intents != null) { if (stickyIntents == null) { stickyIntents = new ArrayList<Intent>(); } stickyIntents.addAll(intents); } } } } } //处理content类型的Intent ArrayList<Intent> allSticky = null; if (stickyIntents != null) { final ContentResolver resolver = mContext.getContentResolver(); // Look for any matching sticky broadcasts... for (int i = 0, N = stickyIntents.size(); i < N; i++) { Intent intent = stickyIntents.get(i); // Don't provided intents that aren't available to instant apps. if (instantApp && (intent.getFlags() & Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) == 0) { continue; } //当前注册广播IntentFilter是否与action一致的intent的匹配 //处理content类型 if (filter.match(resolver, intent, true, TAG) >= 0) { if (allSticky == null) { allSticky = new ArrayList<Intent>(); } allSticky.add(intent); } } } //receiver为null,直接返回null或者第一个粘性intent Intent sticky = allSticky != null ? allSticky.get(0) : null; 编程 if (receiver == null) { return sticky; } synchronized (this) { ... //分析二: //从缓存或新建ReceiverList对象,与Receiver绑定 ReceiverList rl = mRegisteredReceivers.get(receiver.asBinder()); if (rl == null) { rl = new ReceiverList(this, callerApp, callingPid, callingUid, userId, receiver); if (rl.app != null) { final int totalReceiversForApp = rl.app.receivers.size(); if (totalReceiversForApp >= MAX_RECEIVERS_ALLOWED_PER_APP) { throw new IllegalStateException("Too many receivers, total of " + totalReceiversForApp + ", registered for pid: " + rl.pid + ", callerPackage: " + callerPackage); } //添加到ProcessRecord记录中 rl.app.receivers.add(rl); } else { try { receiver.asBinder().linkToDeath(rl, 0); } catch (RemoteException e) { return sticky; } rl.linkedToDeath = true; } mRegisteredReceivers.put(receiver.asBinder(), rl); } ... //新建BroadcastFilter,并添加到BroadcastList BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage, callerFeatureId, permission, callingUid, userId, instantApp, visibleToInstantApps); if (rl.containsFilter(filter)) { ... } else { rhttp://www.devze.coml.add(bf); //添加到接收者解析器 mReceiverResolver.addFilter(bf); } // Enqueue broadcasts for all existing stickies that match // this filter. //分析三:对匹配到action的粘性广播进行广播 if (allSticky != null) { ArrayList receivers = new ArrayList(); receivers.add(bf); final int stickyCount = allSticky.size(); for (int i = 0; i < stickyCount; i++) { Intent intent = allSticky.get(i); BroadcastQueue queue = broadcastQueueForIntent(intent); BroadcastRecord r = new BroadcastRecord(queue, intent, null, null, null, -1, -1, false, null, null, OP_NONE, null, receivers, null, 0, null, null, false, true, true, -1, false, false /* only PRE_BOOT_COMPLETED should be exempt, no stickies */); queue.enqueueParallelBroadcastLocked(r); queue.scheduleBroadcastsLocked(); } } return sticky; } }
广播的解注册
回到ContextWrapper
的unregisterReceiver
函数。
#ContextWrapper public void unregisterReceiver(BroadcastReceiver receiver) { mBase.unregisterReceiver(receiver); } #ContextImpl public void unregisterReceiver(BroadcastReceiver receiver) { if (mPackageInfo != null) { IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher( getOuterContext(), receiver); try { ActivityManager.getService().unregisterReceiver(rd); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } else { throw new RuntimeException("Not supported in system context"); } }
这里通过receiver
和context
获得IIntentReceiver
实例rd
,然后调用AMS的unregisterReceiver
函数。其中LoadedApk
的forgetReceiverDispatcher
函数,主要是从mReceivers
获取IIntentReceiver
的实例,并将receiver
对应的内容从缓存移除。
AMS的unregisterReceiver
函数。主要是将注册过程添加到mRegisteredReceivers
、ProcessProcess
.Receivers、mReceiverResolver
中对应的内容移除。并终止正在发送的广播。
public void unregisterReceiver(IIntentReceiver receiver) { ... final long origId = Binder.clearCallingIdentity(); try { boolean doTrim = false; synchronized(this) { //获得当前对应的ReceiverList ReceiverList rl = mRegist开发者_Go学习eredReceivers.get(receiver.asBinder()); if (rl != null) { //默认情况为null,看看广播发送是否会赋值 //从处理逻辑来看,就是广播内容 final BroadcastRecord r = rl.curBroadcast; if (r != null && r == r.queue.getMatchingOrderedReceiver(r)) { final boolean doNext = r.queue.finishReceiverLocked( r, r.resultCode, r.resultData, r.resultExtras, r.resultAbort, false); if (doNext) { doTrim = true; r.queue.processNextBroadcast(false); } } //从processRecord中移除 if (rl.app != null) { rl.app.receivers.remove(rl); } //从mRegisteredReceivers和mReceiverResolver移除 removeReceiverLocked(rl); if (rl.linkedToDeath) { rl.linkedToDeath = false; rl.receiver.asBinder().unlinkToDeath(rl, 0); } } } // If we actually concluded any broadcasts, we might now be able // to trim the recipients' apps from our working set if (doTrim) { trimApplications(OomAdjuster.OOM_ADJ_REASON_FINISH_RECEIVER); return; } } finally { Binder.restoreCallingIdentity(origId); } } void removeReceiverLocked(ReceiverList rl) { mRegisteredReceivers.remove(rl.receiver.asBinder()); for (int i = rl.size() - 1; i >= 0; i--) { mReceiverResolver.removeFilter(rl.get(i)); } }
广播的发送
定位到ContextWrapper
的sendBroadcast
函数。
public void sendBroadcast(Intent intent) { mBase.sendBroadcast(intent); } public void sendBroadcast(Intent intent) { warnIfCallingFromSystemProcess(); String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); try { intent.prepareToLeaveProcess(this); ActivityManager.getService().broadcastIntentWithFeature(mMainThread.getApplicationThread(), getAttributionTag(), intent, resolvedType,null, Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false,false, getUserId()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }
调用AMS
的broadcastIntentWithFeature
函数。内部又调用了broadcastIntentLocked
函数。是所有Intent处理的地方,很长很长。有对特殊类型的Intent处理,例如Intent.ACTION_PACKAGE_REMOVED
和Intent.ACTION_TIME_CHANGED
。该函数主要将有序和无序广播接收者(匹配Intent)添加到receivers
列表,并创建BroadcastRecord
对象r,持有receivers
列表。并根据intent
获得对应的广播队列queue
,将r
添加到queue
中,执行queue.scheduleBroadcastsLocked
函数。
#AMS xxm @GuardedBy("this") final int broadcastIntentLocked(ProcessRecord callerApp, String callerPackage, @Nullable String callerFeatureId, 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 realCallingUid, int realCallingPid, int userId, boolean allowBackgroundActivityStarts, @Nullable int[] broadcastWhitelist) { intent = new Intent(intent); ... int[] users; if (userId == UserHandle.USER_ALL) { // Caller wants broadcast to go to all started users. users = mUserController.getStartedUserArray(); } else { // Caller wants broadcast to go to one specific user. users = new int[] {userId}; } // Figure out who all will receive this broadcast. List receivers = null; List<BroadcastFilter> registeredReceivers = null; // Need to resolve the intent to interested receivers... if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { receivers = collectReceiverComponents( intent, resolvedType, callingUid, users, broadcastWhitelist); } if (intent.getComponent() == null) { if (userId == UserHandle.USER_ALL && callingUid == SHELL_UID) { // Query one target user at a time, excluding shell-restricted users for (int i = 0; i < users.length; i++) { if (mUserController.hasUserRestriction( UserManager.DISALLOW_DEBUGGING_FEATURES, users[i])) { continue; } //查询已注册的Receiver,在注册过程会被添加 List<BroadcastFilter> registeredReceiversForUser = mReceiverResolver.queryIntent(intent, resolvedType, false /*defaultOnly*/, users[i]); if (registeredReceivers == null) { registeredReceivers = registeredReceiversForUser; } else if (registeredReceiversForUser != null) { http://www.devze.com registeredReceivers.addAll(registeredReceiversForUser); } } } else { //查询已注册的Receiver,在注册过程会被添加 registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false /*defaultOnly*/, userId); } } final boolean replacePending = (intent.getFlags()&Intent编程客栈.FLAG_RECEIVER_REPLACE_PENDING) != 0; ... int NR = registeredReceivers != null ? registeredReceivers.size() : 0; if (!ordered && NR > 0) { ... //通过intent获得广播队列 final BroadcastQueue queue = broadcastQueueForIntent(intent); //将所有数据都封装到BroadcastRecord中 BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp, callerPackage, callerFeatureId, callingPid, callingUid, callerInstantApp, resolvedType, requiredPermissions, appOp, brOptions, registeredReceivers, resultTo, resultCode, resultData, resultExtras, ordered, sticky, false, userId, allowBackgroundActivityStarts, timeoutExempt); ... final boolean replaced = replacePending && (queue.replaceParallelBroadcastLocked(r) != null); if (!replaced) { //将广播添加到queue的mParallelBroadcasts数组列表中 queue.enqueueParallelBroadcastLocked(r); queue.scheduleBroadcastsLocked(); } registeredReceivers = null; NR = 0; } ... return ActivityManager.BROADCAST_SUCCESS; }
enqueueParallelBroadcastLocked
函数将BroadcastRecord
对象r
添加到ArrayList
类型的mParallelBroadcasts
,后续执行队列事务会从其中取出。
scheduleBroadcastsLocked
函数。调用BroadcastHandler
类型的mHandler
发送一个BROADCAST_INTENT_MSG
消息。
#BroadcastQueue xxm public void scheduleBroadcastsLocked() { if (mBroadcastsScheduled) { return; } mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this)); mBroadcastsScheduled = true; }
BroadcastHandler
的handleMessage
函数。执行了processNextBroadcast
函数。
#BroadcastHandler xxm public void handleMessage(Message msg) { switch (msg.what) { case BROADCAST_INTENT_MSG: { processNextBroadcast(true); } break; case BROADCAST_TIMEOUT_MSG: { synchronized (mService) { broadcastTimeoutLocked(true); } } break; } }
processNextBroadcast
函数内容太长了,主要是将粘性广播和无序广播发送给接收者。这里只看函数前半部分对无序广播的处理。其中无序广播是从mParallelBroadcasts
取出所有广播,并遍历每个广播的过滤器filter
,将广播和广播filter
传递给deliverToRegisteredReceiverLocked
函数
#BroadcastQueue xxm if (fromMsg) { mBroadcastsScheduled = false; } //遍历无序广播数组 while (mParallelBroadcasts.size() > 0) { r = mParallelBroadcasts.remove(0); ... final int N = r.receivers.size(); ... for (int i=0; i<N; i++) { Object target = r.receivers.get(i); //开始传递广播 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false, i); } addBroadcastToHistoryLocked(r); }
deliverToRegisteredReceiverLocked
函数。主要进行权限检查。
#BroadcastQueue xxm private void deliverToRegisteredReceiverLocked(BroadcastRecord r, BroadcastFilter filter, boolean ordered, int index) { boolean skip = false; ... //广播filter进行权限检查,不通过skip=true ... if (skip) { r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED; return; } ... r.delivery[index] = BroadcastRecord.DELIVERY_DELIVERED; ... performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, new Intent(r.intent), r.resultCode, r.resultData, r.resultExtras, r.ordered, r.initialSticky, r.userId); ... }
performReceiveLocked
函数。如果接收者所在的进程已经启动,直接调用 app.thread.scheduleRegisteredReceiver
,如果未启动,则直接回调 receiver.performReceive
。
#BroadcastQueue xxm void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, Intent intent, int resultCode, String data, Bundle extras, boolean ordered, boolean sticky, int sendingUser) throws RemoteException { if (app != null) { if (app.thread != null) { try { app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, data, extras, ordered, sticky, sendingUser, app.getReportedProcState()); } catch (RemoteException ex) { synchronized (mService) { app.scheduleCrash("can't deliver broadcast"); } throw ex; } } else { throw new RemoteException("app.thread must not be null"); } } else { receiver.performReceive(intent, resultCode, data, extras, ordered, sticky, sendingUser); } }
如果进程已经启动,则调用ApplicationThread
的scheduleRegisteredReceiver
函数。
public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent, int resultCode, String dataStr, Bundle extras, boolean ordered, boolean sticky, int sendingUser, int processState) throws RemoteException { updateProcessState(processState, false); receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky, sendingUser); }
调用了LoadedApk.ReceiverDispatcher.InnerReceiver
类的performReceive
函数。
public void performReceive(Intent intent, int resultCode, String data, Bundle extras, boolean ordered, boolean sticky, int sendingUser) { final LoadedApk.ReceiverDispatcher rd; if (intent == null) { rd = null; } else { rd = mDispatcher.get(); } if (rd != null) { //分析一 rd.performReceive(intent, resultCode, data, extras, ordered, sticky, sendingUser); } else { IActivityManager mgr = ActivityManager.getService(); try { if (extras != null) { extras.setAllowFds(false); } mgr.finishReceiver(this, resultCode, data, extras, false, intent.getFlags()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } }
调用了分析 一ReceiverDispatcher
的performReceive
函数
public void performReceive(Intent intent, int resultCode, String data, Bundle extras, boolean ordered, boolean sticky, int sendingUser) { //分析1 final Args args = new Args(intent, resultCode, data, extras, ordered, sticky, sendingUser); ... //分析2 if (intent == null || !MACtivityThread.post(args.getRunnable())) { if (mRegistered && ordered) { IActivityManager mgr = ActivityManager.getService(); args.sendFinished(mgr); } } }
ReceiverDispatcher.performReceive
函数中分析1将相关数据封装成内部类Args
类型的args
,然后在分析2通过Handler
类型的mActivityThread
执行getRunnable
返回的Runable
对象的run
函数。这时切换到应用进程的主线程。
Args.getRunnable
函数回调了我们注册广播是复写的onReceiver
函数。
public final Runnable getRunnable() { return () -> { ... ClassLoader cl = mReceiver.getClass().getClassLoader(); intent.setExtrasClassLoader(cl); intent.prepareToEnterProcess(); setExtrasClassLoader(cl); receiver.setPendingResult(this); //调用广播接收者的onReceive receiver.onReceive(mContext, intent); ... } }
总结
大道至简,所谓注册就是在每个地方维持一个集合,实现所谓的增删改查,根据业务需求增加不同逻辑,例如权限检查,接收者所在进程的状态。
以上就是详解android广播Broadcast的启动流程的详细内容,更多关于Android Broadcast启动流程的资料请关注我们其它相关文章!
精彩评论