热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android本地广播LocalBroadcastManager源码解析

序言Broadcast作为Android的四大组件之一,重要性不言而喻;一般我们使用广播的方式通常如下,继承BroadcastReceiver,新建一个广播类。publicclas
序言

Broadcast作为Android的四大组件之一,重要性不言而喻;一般我们使用广播的方式通常如下,继承BroadcastReceiver,新建一个广播类。

public class MyBroadcastReceiver extends BroadcastReceiver {
public static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
}
}

然后在Activity中注册

IntentFilter filter = new IntentFilter();
filter.addAction("ceshi");
registerReceiver(new MyBroadcastReceiver(), filter);

在需要的时候发送

Intent intent = new Intent();
intent.setAction("ceshi");
sendBroadcast(intent);

这个时候就会回调MyBroadcastReceiver的onReceive方法,然后在方法中处理你的逻辑, 但是这种方式的广播是系统级别的,就是说如果我在A,B App中都注册了action=”ceshi”的广播,然后A App发送的action=”ceshi”的广播不止会回调A App中的onReceive方法,也会回调B App中的onReceive方法, 就好比:我们所有的App都注册了系统的Wifi状态切换广播,那Wifi状态有变化的时候,所有App都会收到这条广播。

但是有时候我们的业务需求只需要在自己App中发送接受广播就可以了,没必要用系统级别广播,那还有其它方法可以实现这种需求吗?当然有了,就是我们今天要讲的LocalBroadcastManager,从字面意思就可以看出它是针对本地广播的,那现在让我们通过源码来看看它内部的实现逻辑。

使用

注册方法:

BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(br,new IntentFilter());

发送广播方法:

LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent());

解绑方法:

LocalBroadcastManager.getInstance(this).unregisterReceiver(br);

得到实例方法:

public static LocalBroadcastManager getInstance(Context context) {
synchronized (mLock) {
if (mInstance == null) {
mInstance = new LocalBroadcastManager(context.getApplicationContext());
}
return mInstance;
}
}

从上面方法可以看出LocalBroadcastManager是一个单例全局对象,并且它的几个核心方法和系统级别广播的方法调用是一致的,那下面让我们来通过源码来分析一下它几个核心方法的实现。

源码

注册方法源码

public void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
synchronized (mReceivers) {
ReceiverRecord entry = new ReceiverRecord(filter, receiver);
ArrayList filters = mReceivers.get(receiver);
if (filters == null) {
filters = new ArrayList(1);
mReceivers.put(receiver, filters);
}
filters.add(filter);
for (int i=0; i String action = filter.getAction(i);
ArrayList entries = mActions.get(action);
if (entries == null) {
entries = new ArrayList(1);
mActions.put(action, entries);
}
entries.add(entry);
}
}
}

首先它new了一个ReceiverRecord对象,ReceiverRecord对象里面有两个3个属性

IntentFilter filter;
BroadcastReceiver receiver;
boolean broadcasting;

然后通过mReceivers.get(receiver)得到一个IntentFilter List 集合, mReceivers是一个Map, key是BroadcastReceiver, value是ArrayList,集合为空就new一个然后放到Map中,然后把传进来的IntentFilter对象添加到List集合当中,从这几句代码可以看出来多个IntentFilter可以对应一个BroadcastReceiver;然后传递的IntentFilter action有可能有多个,循环遍历countActions,然后从mActions集合中通过get方法得到ReceiverRecord List集合, mActions是key为action,value为ArrayList的Map,就是说一个action可以对应多个ReceiverRecord对象,然后把刚才new的ReceiverRecord对象添加到ArrayList集合当中,注册方法就完了。

注册成功以后就要使用发送方法来发送广播
发送方法源码:

public boolean sendBroadcast(Intent intent) {
synchronized (mReceivers) {
final String action = intent.getAction();
final String type = intent.resolveTypeIfNeeded(
mAppContext.getContentResolver());
final Uri data = intent.getData();
final String scheme = intent.getScheme();
final Set categories = intent.getCategories();
final boolean debug = DEBUG || ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
if (debug)
Log.v(TAG, "Resolving type " + type + " scheme " + scheme + " of intent " + intent);
ArrayList entries = mActions.get(intent.getAction());
if (entries != null) {
if (debug) Log.v(TAG, "Action list: " + entries);
ArrayList receivers = null;
for (int i=0; i ReceiverRecord receiver = entries.get(i);
if (debug) Log.v(TAG, "Matching against filter " + receiver.filter);

if (receiver.broadcasting) {
if (debug) {
Log.v(TAG, " Filter's target already added");
}
continue;
}
int match = receiver.filter.match(action, type, scheme, data,categories, "LocalBroadcastManager");
if (match >= 0) {
if (debug) Log.v(TAG, " Filter matched! match=0x" + Integer.toHexString(match));

if (receivers == null) {
receivers = new ArrayList();
}
receivers.add(receiver);
receiver.broadcasting = true;
} else {
if (debug) {
String reason;
switch (match) {
case IntentFilter.NO_MATCH_ACTION:
reason = "action"; break;
case IntentFilter.NO_MATCH_CATEGORY:
reason = "category"; break;
case IntentFilter.NO_MATCH_DATA:
reason = "data"; break;
case IntentFilter.NO_MATCH_TYPE:
reason = "type"; break;
default:
reason = "unknown reason"; break;
}
Log.v(TAG, " Filter did not match: " + reason);
}
}
}
if (receivers != null) {
for (int i=0; i receivers.get(i).broadcasting = false;
}
mPendingBroadcasts.add(new BroadcastRecord(intent, receivers));
if (!mHandler.hasMessages(MSG_EXEC_PENDING_BROADCASTS)) {
mHandler.sendEmptyMessage(MSG_EXEC_PENDING_BROADCASTS);
}
return true;
}
}
}
return false;
}

方法的大致逻辑:首先 从mActions Map中根据intent的action得到ReceiverRecord List集合,然后循环集合得到单个ReceiverRecord 对象,如果ReceiverRecord 对象中的filter字段和intent中的属性匹配成功,那么就把该对象添加到新建的receivers List集合中; 最后把intent和receivers 集合封装成BroadcastRecord对象添加到mPendingBroadcasts List集合当中,然后通过handler发送消息,执行executePendingBroadcasts方法

private void executePendingBroadcasts() {
while (true) {
BroadcastRecord[] brs = null;
synchronized (mReceivers) {
final int N = mPendingBroadcasts.size();
if (N <= 0) {
return;
}
brs = new BroadcastRecord[N];
mPendingBroadcasts.toArray(brs);
mPendingBroadcasts.clear();
}
for (int i=0; i BroadcastRecord br = brs[i];
for (int j=0; j br.receivers.get(j).receiver.onReceive(mAppContext, br.intent);
}
}
}
}

这个方法首先把mPendingBroadcasts的数据copy到BroadcastRecord[]数组当中,然后清空mPendingBroadcasts集合,循环遍历数组得到BroadcastRecord 对象,再循环BroadcastRecord 中的receivers集合得到单个ReceiverRecord对象,然后回调ReceiverRecord对象中BroadcastReceiver属性的onReceive方法,onReceive方法就是需要我们重写的方法。

有注册就有解绑,最后我们来看看解绑方法

public void unregisterReceiver(BroadcastReceiver receiver) {
synchronized (mReceivers) {
ArrayList filters = mReceivers.remove(receiver);
if (filters == null) {
return;
}
for (int i=0; i IntentFilter filter = filters.get(i);
for (int j=0; j String action = filter.getAction(j);
ArrayList receivers = mActions.get(action);
if (receivers != null) {
for (int k=0; k if (receivers.get(k).receiver == receiver) {
receivers.remove(k);
k--;
}
}
if (receivers.size() <= 0) {
mActions.remove(action);
}
}
}
}
}
}

首先从mReceivers Map中移除receiver,因为注册的时候放到Map中了,移除返回一个filters集合,然后循环遍历集合,得到filter中的action,在mActions Map中通过action得到receivers集合,这个集合中的单个ReceiverRecord对象的BroadcastReceiver属性如果和要解绑的BroadcastReceiver对象 一样,就移除这个ReceiverRecord对象,最后如果receivers集合中的对象都被移除了,那么就从mActions Map中移除当前的这个action。

到此主要的方法已经分析完毕,可以看出LocalBroadcastManager 内部实现主要依赖Map来保存,遍历,移除数据,是在单个App中进行的,所以以后大家在项目中需要广播的时候多使用本地广播。

谢谢阅读,如果大家感觉本文章对你有用,就麻烦点下喜欢。


推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • Netty源代码分析服务器端启动ServerBootstrap初始化
    本文主要分析了Netty源代码中服务器端启动的过程,包括ServerBootstrap的初始化和相关参数的设置。通过分析NioEventLoopGroup、NioServerSocketChannel、ChannelOption.SO_BACKLOG等关键组件和选项的作用,深入理解Netty服务器端的启动过程。同时,还介绍了LoggingHandler的作用和使用方法,帮助读者更好地理解Netty源代码。 ... [详细]
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
author-avatar
航头党员之家
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有