2024年安卓最新Framework学习(九)Broadcast的注册、发送和接收过程,flutter页面滑动

写在最后

由于本文罗列的知识点是根据我自身总结出来的,并且由于本人水平有限,无法全部提及,欢迎大神们能补充~

将来我会对上面的知识点一个一个深入学习,也希望有童鞋跟我一起学习,一起进阶。

提升架构认知不是一蹴而就的,它离不开刻意学习和思考。

**这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家,**梳理了多年的架构经验,筹备近1个月最新录制的,相信这份视频能给你带来不一样的启发、收获。

最近还在整理并复习一些Android基础知识点,有问题希望大家够指出,谢谢。

希望读到这的您能转发分享和关注一下我,以后还会更新技术干货,谢谢您的支持!

转发+点赞+关注,第一时间获取最新知识点

Android架构师之路很漫长,一起共勉吧!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

public Intent registerReceiver(

BroadcastReceiver receiver, IntentFilter filter) {

return mBase.registerReceiver(receiver, filter);

}

Framework学习(八)Service的启动/绑定过程

这篇文章中我们已经分析了,mBase具体指向就是ContextImpl。

frameworks/base/core/java/android/app/ContextImpl.java

ContextImpl#registerReceiver()

@Override

public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {

return registerReceiver(receiver, filter, null, null);

}

@Override

public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,

String broadcastPermission, Handler scheduler) {

return registerReceiverInternal(receiver, getUserId(),

filter, broadcastPermission, scheduler, getOuterContext()); //1

}

注释1最终会调用到registerReceiverInternal。

ContextImpl#registerReceiverInternal()

private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,

IntentFilter filter, String broadcastPermission,

Handler scheduler, Context context) {

IIntentReceiver rd = null;

if (receiver != null) {

if (mPackageInfo != null && context != null) { //1

if (scheduler == null) {

scheduler = mMainThread.getHandler();

}

rd = mPackageInfo.getReceiverDispatcher(

receiver, context, scheduler,

mMainThread.getInstrumentation(), true); //2

} else {

if (scheduler == null) {

scheduler = mMainThread.getHandler();

}

rd = new LoadedApk.ReceiverDispatcher(

receiver, context, scheduler, null, true).getIIntentReceiver(); //3

}

}

try {

final Intent intent = ActivityManagerNative.getDefault().registerReceiver(

mMainThread.getApplicationThread(), mBasePackageName,

rd, filter, broadcastPermission, userId); //4

if (intent != null) {

intent.setExtrasClassLoader(getClassLoader());

intent.prepareToEnterProcess();

}

return intent;

} catch (RemoteException e) {

throw e.rethrowFromSystemServer();

}

}

注释1处判断如果LoadedApk类型的mPackageInfo不等于null并且context不等null就调用注释2处的代码通过mPackageInfo的getReceiverDispatcher方法获取rd对象,否则就调用注释3处的代码来创建rd对象。

注释2和3的代码的目的都是要获取IIntentReceiver类型的rd对象,IIntentReceiver是一个Binder接口,用于进行跨进程的通信,它的具体实现在LoadedApk.ReceiverDispatcher.InnerReceiver。

frameworks/base/core/java/android/app/LoadedApk.java

ReceiverDispatcher

static final class ReceiverDispatcher {

final static class InnerReceiver extends IIntentReceiver.Stub {

final WeakReference<LoadedApk.ReceiverDispatcher> mDispatcher;

final LoadedApk.ReceiverDispatcher mStrongRef;

InnerReceiver(LoadedApk.ReceiverDispatcher rd, boolean strong) {

mDispatcher = new WeakReference<LoadedApk.ReceiverDispatcher>(rd);

mStrongRef = strong ? rd : null;

}

}

}

继续来看registerReceiverInternal函数的注释4,其实是通过Binder调用的AMS的registerReceiver方法。

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

ActivityManagerService#registerReceiver()

public Intent registerReceiver(IApplicationThread caller, String callerPackage,

IIntentReceiver receiver, IntentFilter filter, String permission, int userId) {

ArrayList stickyIntents = null;

ProcessRecord callerApp = null;

int callingUid;

int callingPid;

synchronized(this) {

Iterator actions = filter.actionsIterator(); //1

if (actions == null) {

ArrayList noAction = new ArrayList(1);

noAction.add(null);

actions = noAction.iterator();

}

// Collect stickies of users

int[] userIds = { UserHandle.USER_ALL, UserHandle.getUserId(callingUid) };

while (actions.hasNext()) {

String action = actions.next();

//2

for (int id : userIds) {

ArrayMap<String, ArrayList> stickies = mStickyBroadcasts.get(id);

if (stickies != null) {

ArrayList intents = stickies.get(action);

if (intents != null) {

if (stickyIntents == null) {

stickyIntents = new ArrayList();

}

stickyIntents.addAll(intents);

}

}

}

}

}

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

// If intent has scheme “content”, it will need to acccess

// provider that needs to lock mProviderMap in ActivityThread

// and also it may need to wait application response, so we

// cannot lock ActivityManagerService here.

if (filter.match(resolver, intent, true, TAG) >= 0) {

if (allSticky == null) {

allSticky = new ArrayList();

}

allSticky.add(intent); //3

}

}

}

// The first sticky in the list is returned directly back to the client.

Intent sticky = allSticky != null ? allSticky.get(0) : null;

if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Register receiver " + filter + ": " + sticky);

if (receiver == null) {

return sticky;

}

synchronized (this) {

if (callerApp != null && (callerApp.thread == null

|| callerApp.thread.asBinder() != caller.asBinder())) {

// Original caller already died

return null;

}

ReceiverList rl = mRegisteredReceivers.get(receiver.asBinder()); //4

if (rl == null) {

rl = new ReceiverList(this, callerApp, callingPid, callingUid,

userId, receiver);

if (rl.app != null) {

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 bf = new BroadcastFilter(filter, rl, callerPackage,

permission, callingUid, userId); //5

rl.add(bf); //6

if (!bf.debugCheck()) {

Slog.w(TAG, “==> For Dynamic broadcast”);

}

mReceiverResolver.addFilter(bf); //7

// Enqueue broadcasts for all existing stickies that match

// this filter.

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, -1, -1, null, null, AppOpsManager.OP_NONE, null, receivers,

null, 0, null, null, false, true, true, -1);

queue.enqueueParallelBroadcastLocked®;

queue.scheduleBroadcastsLocked();

}

}

return sticky;

}

}

注释1处根据传入的IntentFilter类型的filter得到actions列表。

注释2处根据actions列表和userIds(userIds可以理解为应用程序的uid)得到所有的粘性广播的intent,并传入到stickyIntents中。

注释3处将这些粘性广播的intent存入到allSticky列表中,从这里可以看出粘性广播是存储在AMS中的。

注释4处获取ReceiverList列表,ReceiverList继承自ArrayList,用来存储广播接收者。

注释5处创建BroadcastFilter并传入此前创建的ReceiverList,BroadcastFilter用来描述注册的广播接收者。

注释6通过add方法将注释5创建的BroadcastFilter添加到ReceiverList中。

注释7处将BroadcastFilter添加到mReceiverResolver中,这样当AMS接收到广播时就可以从mReceiverResolver中找到对应的广播接收者了。

时序图:

广播的发送


广播有很多类型:无序广播(普通广播)、有序广播、粘性广播和APP内部广播,这里以普通广播为例,来讲解广播的发送过程。

要发送普通广播需要调用sendBroadcast方法,它的实现同样在ContextWrapper中:

frameworks/base/core/java/android/content/ContextWrapper.java

ContextWrapper#sendBroadcast()

@Override

public void sendBroadcast(Intent intent) {

mBase.sendBroadcast(intent);

}

frameworks/base/core/java/android/app/ContextImpl.java

ContextImpl#sendBroadcast()

@Override

public void sendBroadcast(Intent intent) {

warnIfCallingFromSystemProcess();

String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());

try {

intent.prepareToLeaveProcess(this);

//1

ActivityManagerNative.getDefault().broadcastIntent(

mMainThread.getApplicationThread(), intent, resolvedType, null,

Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,

getUserId());

} catch (RemoteException e) {

throw e.rethrowFromSystemServer();

}

}

注释1处最终会调用AMS的broadcastIntent方法。

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

ActivityManagerService#broadcastIntent()

public final int broadcastIntent(IApplicationThread caller,

Intent intent, String resolvedType, IIntentReceiver resultTo,

int resultCode, String resultData, Bundle resultExtras,

String[] requiredPermissions, int appOp, Bundle bOptions,

boolean serialized, boolean sticky, int userId) {

synchronized(this) {

intent = verifyBroadcastLocked(intent); //1

final ProcessRecord callerApp = getRecordForAppLocked(caller);

final int callingPid = Binder.getCallingPid();

final int callingUid = Binder.getCallingUid();

final long origId = Binder.clearCallingIdentity();

//2

int res = broadcastIntentLocked(callerApp,

callerApp != null ? callerApp.info.packageName : null,

intent, resolvedType, resultTo, resultCode, resultData, resultExtras,

requiredPermissions, appOp, bOptions, serialized, sticky,

callingPid, callingUid, userId);

Binder.restoreCallingIdentity(origId);

return res;

}

}

注释1处通过verifyBroadcastLocked方法验证广播是否合法。

注释2处调用broadcastIntentLocked方法。

先看看注释1的verifyBroadcastLocked方法:

ActivityManagerService#verifyBroadcastLocked()

final Intent verifyBroadcastLocked(Intent intent) {

// Refuse possible leaked file descriptors

if (intent != null && intent.hasFileDescriptors() == true) { //1

throw new IllegalArgumentException(“File descriptors passed in Intent”);

}

int flags = intent.getFlags(); //2

if (!mProcessesReady) {

// if the caller really truly claims to know what they’re doing, go

// ahead and allow the broadcast without launching any receivers

if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) { //3

// This will be turned into a FLAG_RECEIVER_REGISTERED_ONLY later on if needed.

} else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { //4

Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent

  • " before boot completion");

throw new IllegalStateException(“Cannot broadcast before boot completed”);

}

}

if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {

throw new IllegalArgumentException(

“Can’t use FLAG_RECEIVER_BOOT_UPGRADE here”);

}

return intent;

}

注释1处验证intent是否不为null并且有文件描述符。

注释2处获得intent中的flag。

注释3处如果系统正在启动过程中,判断如果flag设置为FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT(启动检查时只接受动态注册的广播接收者)则不做处理,如果不是则在注释4处判断如果flag没有设置为FLAG_RECEIVER_REGISTERED_ONLY(只接受动态注册的广播接收者)则会抛出异常。

继续来看broadcastIntent的注释2。

ActivityManagerService#broadcastIntentLocked()

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

// Figure out who all will receive this broadcast.

List receivers = null;

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

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

// Query one target user at a time, excluding shell-restricted users

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

List registeredReceiversForUser =

mReceiverResolver.queryIntent(intent,

resolvedType, false, users[i]); //1

if (registeredReceivers == null) {

registeredReceivers = registeredReceiversForUser;

} else if (registeredReceiversForUser != null) {

registeredReceivers.addAll(registeredReceiversForUser);

}

}

} else {

registeredReceivers = mReceiverResolver.queryIntent(intent,

resolvedType, false, userId);

}

}

// Merge into one list.

int ir = 0;

if (receivers != null) {

int NT = receivers != null ? receivers.size() : 0;

int it = 0;

ResolveInfo curt = null;

BroadcastFilter curr = null;

//2

while (it < NT && ir < NR) {

if (curt == null) {

curt = (ResolveInfo)receivers.get(it);

}

if (curr == null) {

curr = registeredReceivers.get(ir);

}

if (curr.getPriority() >= curt.priority) {

// Insert this broadcast record into the final list.

receivers.add(it, curr);

ir++;

curr = null;

it++;

NT++;

} else {

// Skip to the next ResolveInfo in the final list.

it++;

curt = null;

}

}

}

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

//3

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

callerPackage, callingPid, callingUid, resolvedType,

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

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

if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Enqueueing ordered broadcast " + r

  • ": prev had " + queue.mOrderedBroadcasts.size());

if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST,

"Enqueueing broadcast " + r.intent.getAction());

boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked®;

if (!replaced) {

queue.enqueueOrderedBroadcastLocked®;

queue.scheduleBroadcastsLocked(); //4

}

} else {

}

return ActivityManager.BROADCAST_SUCCESS;

}

代码很长,我们这里只捡重点看。

注释1通过mReceiverResolver查询到所有符合条件的广播接收器,这些接收器是在广播注册的时候放到mReceiverResolver中的,具体可以参考广播注册过程。

最后看一下学习需要的所有知识点的思维导图。在刚刚那份学习笔记里包含了下面知识点所有内容!文章里已经展示了部分!如果你正愁这块不知道如何学习或者想提升学习这块知识的学习效率,那么这份学习笔记绝对是你的秘密武器!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • queue.mOrderedBroadcasts.size());

if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST,

"Enqueueing broadcast " + r.intent.getAction());

boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked®;

if (!replaced) {

queue.enqueueOrderedBroadcastLocked®;

queue.scheduleBroadcastsLocked(); //4

}

} else {

}

return ActivityManager.BROADCAST_SUCCESS;

}

代码很长,我们这里只捡重点看。

注释1通过mReceiverResolver查询到所有符合条件的广播接收器,这些接收器是在广播注册的时候放到mReceiverResolver中的,具体可以参考广播注册过程。

最后看一下学习需要的所有知识点的思维导图。在刚刚那份学习笔记里包含了下面知识点所有内容!文章里已经展示了部分!如果你正愁这块不知道如何学习或者想提升学习这块知识的学习效率,那么这份学习笔记绝对是你的秘密武器!

[外链图片转存中…(img-yfz9Ukih-1715017588411)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值