2024年Java最全BroadcastReceiver源码分析,手把手教你写

   日期:2024-12-26    作者:prwon 移动:http://oml01z.riyuangf.com/mobile/quote/52317.html

最后

做任何事情都要用心,要非常关注细节。看起来不起眼的、繁琐的工作做透了会有意想不到的价值。
当然要想成为一个技术大牛也需要一定的思想格局,思想决定未来你要往哪个方向去走, 建议多看一些人生规划方面的书籍,多学习名人的思想格局,未来你的路会走的更远。

更多的技术点思维导图我已经做了一个整理,涵盖了当下互联网最流行99%的技术点,在这里我将这份导图分享出来,以及为金九银十准备的一整套面试体系,上到集合,下到分布式微服务

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

}

2.3 AMS发送到队列处理广播


queue.scheduleBroadcastsLocked();会发送BROADCAST_INTENT_MSG,调用processNextBroadcast(true);

BroadcastQueue.java

final void processNextBroadcast(boolean fromMsg) {

synchronized (mService) {

processNextBroadcastLocked(fromMsg, false);

}

}

final void processNextBroadcastLocked(boolean fromMsg, boolean skipOomAdj){

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

}

}

}

private void deliverToRegisteredReceiverLocked(BroadcastRecord r,

BroadcastFilter filter, boolean ordered, int index) {

··· //分发广播到客户端

performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,

new Intent(r.intent), r.resultCode, r.resultData,

r.resultExtras, r.ordered, r.initialSticky, r.userId);

····

}

void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,

Intent intent, int resultCode, String data, Bundle extras,

boolean ordered, boolean sticky, int sendingUser) throws RemoteException {

// Send the intent to the receiver asynchronously using one-way binder calls.

//判断应是否为空

if (app != null) {

if (app.thread != null) {

//通知客户端分发广播,Binder采用 oneway 异步 应用端会自动串行化

app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,

data, extras, ordered, sticky, sendingUser, app.repProcState);

}

} else {

receiver.performReceive(intent, resultCode, data, extras, ordered,

sticky, sendingUser);

}

}

2.4 客户端处理receiver


AMS端的app.thread.scheduleRegisteredReceiver(),会调用到注册时注册的InnerReceiver的performReceive方法

ActivityThread.java

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

//1

receiver.performReceive(intent, resultCode, dataStr, extras, ordered,

sticky, sendingUser);

}

InnerReceiver.java

public void performReceive(Intent intent, int resultCode, String data,

Bundle extras, boolean ordered, boolean sticky, int sendingUser) {

final LoadedApk.ReceiverDispatcher rd;

···

if (rd != null) {

//调用LoadedApk.ReceiverDispatcher的performReceive

rd.performReceive(intent, resultCode, data, extras,

ordered, sticky, sendingUser);

···

}

}

···

LoadedApk.ReceiverDispatcher.java

public void performReceive(Intent intent, int resultCode, String data,

Bundle extras, boolean ordered, boolean sticky, int sendingUser) {

final Args args = new Args(intent, resultCode, data, extras, ordered,

sticky, sendingUser);

···

//向ActivityThread发送一个Runnable

if (intent == null || !mActivityThread.post(args.getRunnable())) {

}

}

Args.java

public final Runnable getRunnable() {

return () -> {

final BroadcastReceiver receiver = mReceiver;

final boolean ordered = mOrdered;

=

final IActivityManager mgr = ActivityManager.getService();

···

receiver.setPendingResult(this);

//这里触发了BoradcastReciever回调

receiver.onReceive(mContext, intent);

···

};

}

三、动态注册解析总结

=========================================================================

1、首先动态注册BroadcastReceiver到AMS,将他们存储在一个Map中,Map的key是IIntentReceiver用于回调注册端,value是一个ReceiverList

2、发送这发送Intent到AMS,筛选出匹配的ReceiverList,遍历通知注册端

四、BroadcastReceiver静态注册接收

=======================================================================================

4.1 PMS解析清单文件获取注册信息


PackageParser.java

private boolean parseBaseApplication(Package owner, Resources res,

XmlResourceParser parser, int flags, String[] outError){

else if (tagName.equals(“receiver”)) {

Activity a = parseActivity(owner, res, parser, flags, outError, cachedArgs,

true, false);

owner.receivers.add(a);

}

}

1、将receiver解析成一个Activity(这个Activity表示的是一个组件)对象,添加到receivers的列表中,receivers是一个ArrayList

五、普通广播发送后的静态接收

============================================================================

5.1 调用Context的sendBroadcast


最后会调用到

ContextImpl.java

public void sendBroadcast(Intent intent) {

warnIfCallingFromSystemProcess();

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

intent.prepareToLeaveProcess(this);

ActivityManager.getService().broadcastIntent(

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

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

getUserId());

}

5.2 AMS处理broadcastIntent


ActivityManagerService.java

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

···

int res = broadcastIntentLocked(callerApp,

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

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

requiredPermissions, appOp, bOptions, serialized, sticky,

callingPid, callingUid, userId);

···

}

}

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

···

//获取接收静态广播

receivers = collectReceiverComponents(intent, resolvedType, callingUid, users);

//通过Intent查找能接受的动态广播

registeredReceivers = mReceiverResolver.queryIntent(intent,

resolvedType, false /defaultOnly/, userId);

int NR = registeredReceivers != null ? registeredReceivers.size() : 0;

if (!ordered && NR > 0) {

//处理动态广播,添加到并行分发队列,OneWay发送binder处理是串行的

}

//给没有处理完的动态receiver(order是true,跟静态receiver合并到一起

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

//处理剩下的receiver。加到串行分发队列

//获取queue

BroadcastQueue queue = broadcastQueueForIntent(intent);

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

callerPackage, callingPid, callingUid, callerInstantApp, resolvedType,

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

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

//添加到串行分发queue

queue.enqueueOrderedBroadcastLocked®;

//处理分发

queue.scheduleBroadcastsLocked();

}

return ActivityManager.BROADCAST_SUCCESS;

}

5.3 AMS发送到队列处理广播


queue.scheduleBroadcastsLocked();会发送BROADCAST_INTENT_MSG,调用processNextBroadcast(true);

BroadcastQueue.java

final void processNextBroadcast(boolean fromMsg) {

synchronized (mService) {

processNextBroadcastLocked(fromMsg, false);

}

}

final void processNextBroadcastLocked(boolean fromMsg, boolean skipOomAdj){

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

}

}

//如果有pedding广播,先直接返回,这个广播在等待应用进程启动

//如果当前广播分发超时了,废弃这个广播,处理下一个广播

broadcastTimeoutLocked(false);

//如果没有超时,并且在分发中,就先返回。什么也不做

//如果当前的广播已经分发完一个receiver,就继续分发下一个receiver

//如果这个receiver是动态注册的receiver就直接分发

//如果这个receiver是静态注册的receiver,先看进程启动没有

//如果进程启动了,就直接分发

processCurBroadcastLocked(r, app, skipOomAdj);

//没启动的话就先启动进程,然后给广播标记为pedding

//进程启动后attachApplication时继续处理这个pending的广播

}

BroadcastQueue.java

//处理超时

final void broadcastTimeoutLocked(boolean fromMsg) {

BroadcastRecord r = mOrderedBroadcasts.get(0);

Object curReceiver = r.receivers.get(r.nextReceiver-1);

//找到当前分发receiver对应的进程

if (mPendingBroadcast == r) {

mPendingBroadcast = null;

}

// Move on to the next receiver.

finishReceiverLocked(r, r.resultCode, r.resultData,

r.resultExtras, r.resultAbort, false);

//发送消息处理下一个receiver

scheduleBroadcastsLocked();

if (!debugging && anrMessage != null) {

// Post the ANR to the handler since we do not want to process ANRs while

// potentially holding our lock.

//超时显示ANR

mHandler.post(new AppNotResponding(app, anrMessage));

}

}

BroadcastQueue.java

//接收进程存在,通知接收进程

private final void processCurBroadcastLocked(BroadcastRecord r,

ProcessRecord app, boolean skipOomAdj) throws RemoteException {

···

app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,

mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo),

r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId,

app.repProcState);

···

}

5.4 接收进程接收广播


5.4.1 进程存在直接处理

ActivityThread.java

public final void scheduleReceiver(Intent intent, ActivityInfo info,

CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,

boolean sync, int sendingUser, int processState) {

updateProcessState(processState, false);

ReceiverData r = new ReceiverData(intent, resultCode, data, extras,

sync, false, mAppThread.asBinder(), sendingUser);

r.info = info;

r.compatInfo = compatInfo;

sendMessage(H.RECEIVER, r);

}

private void handleReceiver(ReceiverData data) {

sCurrentBroadcastIntent.set(data.intent);

receiver.setPendingResult(data);

//执行BroadcastReceiver

//依据Application的Context创建的ContextWarpper,目的是不允许BroadcastReceiver在注册接收器,也不允许启动服务

receiver.onReceive(context.getReceiverRestrictedContext(),

data.intent);

if (receiver.getPendingResult() != null) {

//通知AMS分发结束

data.finish();

}

}

BroadcastReceiver.java

public final void finish() {

if (mType == TYPE_COMPONENT) {

final IActivityManager mgr = ActivityManager.getService();

if (QueuedWork.hasPendingWork()) {

QueuedWork.queue(new Runnable() {

@Override public void run() {

if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,

"Finishing broadcast after work to component " + mToken);

sendFinished(mgr);

}

}, false);

} else {

sendFinished(mgr);

}

} else if (mOrderedHint && mType != TYPE_UNREGISTERED) {

final IActivityManager mgr = ActivityManager.getService();

sendFinished(mgr);

}

}

public void sendFinished(IActivityManager am) {

//通知AMS分发结束

看完美团、字节、腾讯这三家的面试问题,是不是感觉问的特别多,可能咱们又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。

开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题

注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友

互联网工程师必备的面试1000题

而且从上面三家来看算法与数据结构是必备不可少的,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

final IActivityManager mgr = ActivityManager.getService();

sendFinished(mgr);

}

}

public void sendFinished(IActivityManager am) {

//通知AMS分发结束

看完美团、字节、腾讯这三家的面试问题,是不是感觉问的特别多,可能咱们又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。

开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题

注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友

[外链图片转存中…(img-gReWvylz-1714849681530)]

互联网工程师必备的面试1000题

而且从上面三家来看算法与数据结构是必备不可少的,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题

[外链图片转存中…(img-I1bS0Tjj-1714849681530)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录


特别提示:本信息由相关用户自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


举报收藏 0评论 0
0相关评论
相关最新动态
推荐最新动态
点击排行
{
网站首页  |  关于我们  |  联系方式  |  使用协议  |  隐私政策  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2020018471号