[ Android源码分析 ] 动态注册在源码中是如何实现的

[ Android源码分析 ] 动态注册在源码中是如何实现的

尊重原创,转载请注明出处!

前言

前几天刚分析完静态注册的源码 [ Android源码分析 ] 静态注册在源码中是如何实现的 。静态注册的源码分析过了,动态注册的流程分析自然也得跟上。

应用实现方式

动态注册在应用中是通过调用 ContextWrapper.registerReceiver 来实现的。代码实现方式如下:

IntentFilter filter = new IntentFilter();
filter.addAction("com.example.broadcast");
BroadcastReceiver receiver = new BroadcastReceiver() {
	@Override
	public void onReceive(Context context, Intent intent) {
		// do something
	}
};
registerReceiver(receiver , filter);

注意:一般 receiver 不会声明为临时变量,因为除了进入应用后需要调用 registerReceiver 来注册接收器,退出应用后还需要调用 unregisterReceiver 来注销广播接收器。当然,这是比较基础的东西了。

动态注册调用流程

应用所调用的 Context 的方法,一般在 ContextWrapper 中做了一层封装。

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

@Override
public Intent registerReceiver(
    BroadcastReceiver receiver, IntentFilter filter) {
    return mBase.registerReceiver(receiver, filter);
}

ContextWrapper 中的方法基本都在 ContextImpl 中实现,通过封装调用流程,最终走到 AMS 中。

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

@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(), 0);
}

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();
            }
            // 因为是跨进程传输,这里将BroadcastReceiver封装为IIntentReceiver.Stub的形式,发送广播后通过rd最终回调到BroadcastReceiver.onReceive
            rd = mPackageInfo.getReceiverDispatcher(
                receiver, context, scheduler,
                mMainThread.getInstrumentation(), true);
        } else {
            if (scheduler == null) {
                scheduler = mMainThread.getHandler();
            }
            // 因为是跨进程传输,这里将BroadcastReceiver封装为IIntentReceiver.Stub的形式
            rd = new LoadedApk.ReceiverDispatcher(
                    receiver, context, scheduler, null, true).getIIntentReceiver();
        }
    }
    try {
    	// 调用AMS中的registerReceiver实现
        final Intent intent = ActivityManager.getService().registerReceiver(
                mMainThread.getApplicationThread(), mBasePackageName, rd, filter,
                broadcastPermission, userId, flags);
        if (intent != null) {
            intent.setExtrasClassLoader(getClassLoader());
            intent.prepareToEnterProcess();
        }
        return intent;
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}

动态注册最终的实现还是在 ActivityManagerService 的 registerReceiver 方法中。

public Intent registerReceiver(IApplicationThread caller, String callerPackage,
        IIntentReceiver receiver, IntentFilter filter, String permission, int userId,
        int flags) {

	// 遍历IntentFilter中的action,看mStickyBroadcasts中是否存在对应的粘性广播,如果有,则暂存到allSticky中,等待后续处理,不是重点,所以代码不放了
    ......

	// 这里有点意思,registerReceiver返回的是第一个匹配上的粘性广播,以前还真没留意过
    // The first sticky in the list is returned directly back to the client.
    Intent sticky = allSticky != null ? allSticky.get(0) : null;
    ......

    synchronized (this) {
        ......
        // 尝试从系统缓存的静态注册Receiver列表中获取此Receiver
        ReceiverList rl = mRegisteredReceivers.get(receiver.asBinder());
        if (rl == null) {
            rl = new ReceiverList(this, callerApp, callingPid, callingUid,
                    userId, receiver);
            ......
            // 如果mRegisteredReceivers中不存在此Receiver,则记录动态注册的Receiver到mRegisteredReceivers中,unregister时也会从这里移除
            mRegisteredReceivers.put(receiver.asBinder(), rl);
        } else if (rl.uid != callingUid) {
            throw new IllegalArgumentException(
                    "Receiver requested to register for uid " + callingUid
                    + " was previously registered for uid " + rl.uid
                    + " callerPackage is " + callerPackage);
        } else if (rl.pid != callingPid) {
            throw new IllegalArgumentException(
                    "Receiver requested to register for pid " + callingPid
                    + " was previously registered for pid " + rl.pid
                    + " callerPackage is " + callerPackage);
        } else if (rl.userId != userId) {
            throw new IllegalArgumentException(
                    "Receiver requested to register for user " + userId
                    + " was previously registered for user " + rl.userId
                    + " callerPackage is " + callerPackage);
        }
        // 构造BroadcastFilter,发送广播时判断如果为BroadcastFilter,则认为是动态注册
        BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,
                permission, callingUid, userId, instantApp, visibleToInstantApps);
        rl.add(bf);
        if (!bf.debugCheck()) {
            Slog.w(TAG, "==> For Dynamic broadcast");
        }
        // 这里是最关键的地方!将BroadcastFilter添加到mReceiverResolver,后续发送广播时的查询也是通过调用mReceiverResolver的queryIntent去查询对应的Receiver
        mReceiverResolver.addFilter(bf);

        // 还记得前面暂存到allSticky的粘性广播吗?最后遍历allSticky进行接收处理,注意是并行处理~
        if (allSticky != null) {
            ArrayList receivers = new ArrayList();
            receivers.add(bf);

            final int stickyCount = allSticky.size();
            for (int i = 0; i < stickyCount; i++) {
            	// 遍历此Receiver匹配上的黏性广播,并行分发处理
                Intent intent = allSticky.get(i);
                BroadcastQueue queue = broadcastQueueForIntent(callerPackage, intent);
                BroadcastRecord r = new BroadcastRecord(queue, intent, null,
                        null, -1, -1, false, null, null, AppOpsManager.OP_NONE, null, receivers,
                        null, 0, null, null, false, true, true, -1);
                queue.enqueueParallelBroadcastLocked(r);
                queue.scheduleBroadcastsLocked();
            }
        }

        return sticky;
    }
}

执行完 AMS 中的 registerReceiver 后,发送广播时就能通过接口查询到系统中注册过的广播,进行分发和处理。

发送广播给动态注册接收器

在之前写的博客 [ Android实战 ] 开机时通过广播启动应用,但是很长时间才能接收到,如何解决?中,分析过 Android 发送广播的流程。应用调用 sendBroadcast 后,经过调用流程,走到 AMS 里面的 broadcastIntentLocked 方法。

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

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 == SHELL_UID) {
            ......
        } else {
        	// 这里获取动态注册了该intent的receiver,放到registeredReceivers队列中
            registeredReceivers = mReceiverResolver.queryIntent(intent,
                    resolvedType, false /*defaultOnly*/, userId);
        }
    }
    ......
}

可以看到,动态注册的 receiver 是通过调用 mReceiverResolver.queryIntent 查询到的。也就和前面分析的流程结合起来了。

总结

最后简单做下总结:

Android 在调用 registerReceiver 时,会通过调用流程最终走到 AMS 中,将注册的接收器封装为 BroadcastFilter 对象,并 add 到 mReceiverResolver 中。

发送广播时,会调用 mReceiverResolver.queryIntent 查询注册了该广播的动态注册接收器,再进行分发处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值