Android开发事件总线之EventBus运用和框架原理深入理解
[Android]事件总线之EventBus的使用背景
在我们的android项目开发过程中,经常会有各个组件如activity,fragment和service之间,各个线程之间的通信需求;项目中用的最多的是Android框架的广播机制,android的广播机制是基于系统的Binder机制实现IPC或者进程内部的通信,而Binder这种IPC机制相比于Linux原有的机制来说具有,性能更好、安全性更高和易用性更好的特点,所以android系统中很多系统事件都是基于广播的方式来发送,如开机广播、电量低的提醒广播等。然而在一个android的进程内部,各个组件、子线程之间通信如果使用广播的话,那就有种杀鸡用牛刀的赶脚。。。
[Android]进程内部通信之EventBus
EventBus是greenrobot开发的发布/订阅事件总线组件,也是基于观察者模式,不同点在于,EventBus框架解耦了事件的发送和订阅模块,事件对象的分发交由EventBus来处理,下面的框架图可以清晰的看到这一点。

[Android]EventBus 3.0基本使用方法
一、定义事件类
作为事件的发布者,需要定义所发布的事件的类:
public class MessageEvent {
private String msg;
public MessageEvent(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
二、注册/取消注册响应事件
作为事件的订阅者,需要把响应事件的对象注册到EventBus当中:
EventBus.getDefault().register(obj)
当不需要处理某个类型的事件的时候,取消对这个事件的监听:
EventBus.getDefault().unregister(obj)
三、声明和注释订阅方法,选择指定线程模式
作为事件的订阅者,需要定义事件的响应方法,方法名称可以随意取,方法的形参类型,必须和监听的事件对象类型一致:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
Toast.makeText(this, event.getMsg (),
Toast.LENGTH_SHORT).show();
}
3.1 四种线程模式
事件订阅者可以通过注解的方式选择处理事件的方法所在的线程:
PostThread:如果事件处理函数指定了线程模型为PostThread,那么事件的发布和接收处理会在同一个线程当中。
BackgroundThread:如果事件处理函数指定了线程模型为BackgroundThread,那么如果事件是在UI线程中发布出来的,那么该事件处理函数就会在新的子线程中运行,如果事件发布本来就是非UI线程中发布出来 的,那么该事件处理函数直接在发布事件的线程中执行。
MainThread:如果事件处理函数指定了线程模型为MainThread,那么不论事件对象是在哪个线程中发布出来的,该事件处理函数都会在UI线程中执行。
Async:如果事件处理函数指定了线程模型为Async,那么无论事件在哪个线程发布,该事件处理函数都会在新建的子线程中执行。
3.2 黏性事件
通过注解的方式设置sticky为true,那么事件处理函数则可以处理上一次的事件对象:
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
四、EventBus 3.0源码详解
4.1 注册流程
/**
* Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
* are no longer interested in receiving events.
* <p/>
* Subscribers have event handling methods that must be annotated by {@link Subscribe}.
* The {@link Subscribe} annotation also allows configuration like {@link
* ThreadMode} and priority.
*/
public void register(Object subscriber) {
//通过注册的对象得到其类的class对象
Class<?> subscriberClass = subscriber.getClass();
//通过类的class对象得到此对象的订阅方法列表
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod :subscriberMethods) {
//线程同步,遍历订阅方法列表,注册每一个订阅方法
subscribe(subscriber, subscriberMethod);
}
}
}
代码subscriberMethodFinder.findSubscriberMethods(subscriberClass)获取订阅方法列表具体如下:
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
//在缓存中查找此class对象对应的订阅方法列表
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
//是否忽略注解器生成的MyEventBusIndex类
if (ignoreGeneratedIndex) {
//通过反射机制得到订阅者类class对象对应的订阅事件方法列表
subscriberMethods = findUsingReflection(subscriberClass);
} else {
//从注解器生成的MyEventBusIndex类中获得订阅类的订阅方法列表
subscriberMethods = findUsingInfo(subscriberClass);
}
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe annotation");
} else {
//缓存此class对象的订阅方法列表
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
通过反射机制获取订阅方法列表:
private List<SubscriberMethod> findUsingReflection(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
//遍历当前class对象和其父类中的订阅方法
findUsingReflectionInSingleClass(findState);
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
findUsingReflectionInSingleClass方法:
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
//遍历此类的方法
for (Method method : methods) {
int modifiers = method.getModifiers();
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
//形参只有一个的函数
if (parameterTypes.length == 1) {
//得到此函数的注解信息对象
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException("@Subscribe method " + methodName +
"must have exactly 1 parameter but has " + parameterTypes.length);
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
}
}
}
至此,我们得到了所有的订阅函数列表,下一步,会对每一个订阅函数进行注册:
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
Class<?> eventType = subscriberMethod.eventType;
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
//通过监听的事件对象得到其对应的监听者对象列表
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
//如果这个对象已经在此事件对象的监听者列表当中,则抛出异常
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
//遍历监听者列表,根据订阅方法的优先级将新的监听对象插入到列表的指定位置,即订阅方法的注册
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
//通过订阅者对象得到其对应的监听事件类型列表
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
//添加事件类型对象到此监听对象对应的事件对象列表当中
subscribedEvents.add(eventType);
//在这里处理黏性事件
if (subscriberMethod.sticky) {
if (eventInheritance) {
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
Class<?> candidateEventType = entry.getKey();
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
//将黏性事件发送到指定的订阅方法当中处理
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
//将黏性事件发送到指定的订阅方法当中处理
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
事件的注册流程实际是从监听者对象和消息事件两个维度,将对方分别添加到自己对应的列表当中,具体可以通过以下流程图总结:

4.2发布流程
/** Posts the given event to the event bus. */
public void post(Object event) {
//通过ThreadLocal机制得到当前线程的postingState对象
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
//在此线程的eventQueue中添加此事件对象
eventQueue.add(event);
if (!postingState.isPosting) {
//判断当前线程是否UI线程
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
while (!eventQueue.isEmpty()) {
//遍历此线程消息队列,处理消息队列中的消息事件
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
ThreadLocal机制可以存储各个线程的局部数据;
postSingleEvent函数处理此线程消息队列中的消息事件:
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
if (eventInheritance) {
//得到此事件类的所有父类和接口
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
//通过事件类得到其对应的订阅者对象列表,将事件对象分发到相应的订阅函数中处理,至此实现了事件消息的传递
subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
}
} else {
//通过事件类得到其对应的订阅者对象列表,将事件对象分发到相应的订阅函数中处理,至此实现了事件消息的传递
subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
}
if (!subscriptionFound) {
if (logNoSubscriberMessages) {
Log.d(TAG, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
事件消息对象具体的分发函数:postToSubscription
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
//根据注解方式设置的线程模式,在不同的线程中执行订阅函数
switch (subscription.subscriberMethod.threadMode) {
case POSTING:
invokeSubscriber(subscription, event);
break;
case MAIN:
if (isMainThread) {
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case BACKGROUND:
if (isMainThread) {
backgroundPoster.enqueue(subscription, event);
} else {
invokeSubscriber(subscription, event);
}
break;
case ASYNC:
asyncPoster.enqueue(subscription, event);
break;
default:
throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
}
}
至此,我们完成了事件消息对象的分发流程,以下流程图来总结post的过程:

4.3 取消注册流程
/** Unregisters the given subscriber from all event classes. */
public synchronized void unregister(Object subscriber) {
//根据订阅者对象得到其对应的事件类型列表
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
//遍历事件类型列表,得到每个事件类型对应的订阅者对象列表,遍历这个列表,如果是此观察者对象,则从列表中删除
unsubscribeByEventType(subscriber, eventType);
}
//typesBySubscriber中删除此订阅者对象
typesBySubscriber.remove(subscriber);
} else {
Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
unsubscribeByEventType函数:
/** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
//根据事件类型得到其对应的订阅者对象列表
List<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions != null) {
int size = subscriptions.size();
for (int i = 0; i < size; i++) {
Subscription subscription = subscriptions.get(i);
//遍历列表,如果找到此订阅者对象则从列表中删除
if (subscription.subscriber == subscriber) {
subscription.active = false;
subscriptions.remove(i);
i--;
size--;
}
}
}
}
取消注册的流程总结如下:
1.通过观察者类对象通过MAP表得到其对应的事件类class对象列表.
2.遍历list列表,通过事件类class对象得到其在MAP表中对应的观察者类对象列表。
3.遍历此观察者对象列表,判断如果列表中存在需要取消的注册观察者对象,则从对象列表中删除此观察者对象。
4.从第1步中得到的MAP对象中删除以取消注册的观察者对象为key的映射项目。
5.完成unregister过程。
五、相似功能框架的比较
和EventBus具有相似功能的组件有Square推出的Otto,Android系统的LocalBroadCast,共同点是这三个框架都是进程内通信,方便进程内各个组件和子线程间的通信。
Ottootto采用了注解的方式完成注册,Otto更多使用场景应该就是在主线程中,因为其不像EventBus可以选择不同的线程模式。
LocalBroadCast是Android在Android Support Library中提供的,用于进程内部通信框架,其主要目的是保证广播在进程内部传递,确保通讯的安全行,其内部通过handler的方式实现。
Android开发事件总线之EventBus运用和框架原理深入理解的更多相关文章
- Android学习系列(43)--使用事件总线框架EventBus和Otto
事件总线框架 针对事件提供统一订阅,发布以达到组件间通信的解决方案. 原理 观察者模式. EventBus和Otto 先看EventBus的官方定义: Android optimized event ...
- 【Android】事件总线(解耦组件) EventBus 详解
当Android项目越来越庞大的时候,应用的各个部件之间的通信变得越来越复杂,例如:当某一条件发生时,应用中有几个部件对这个消息感兴趣,那么我们通常采用的就是观察者模式,使用观察者模式有一个弊病就是部 ...
- Vue事件总线(eventBus)$on()会多次触发解决办法
项目中使用了事件总线eventBus来进行两个组件间的通信, 使用方法是是建立eventBus.js文件,暴露一个空的Vue实例,如下: import Vue from 'vue'export def ...
- Android开发——事件分发机制详解
0. 前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52566965 深入学习事件分发机制,是为了解决在Android开发中 ...
- Android开发学习之路-EventBus使用
EventBus是一个通过发布.订阅事件实现组件间消息传递的工具. 它存在的目的,就是为了优化组件之间传递消息的过程.传统组件之间传递消息的方法有使用广播,回调等,而这些方法使用都比较复杂. 工作原理 ...
- android开发事件监听
第一种:匿名内部类作为事件监听器类 大部分时候,事件处理器都没有什么利用价值(可利用代码通常都被抽象成了业务逻辑方法),因此大部分事件监听器只是临时使用一次,所以使用匿名内部类形式的事件监听器更合适, ...
- 事件总线(EventBus)
Vue.prototype.$EventBus = new Vue() 不建议用,尽量用vuex,eventbus过于消耗浏览器资源 傻瓜版状态管理 一般的状态传递是在同时显示的情况下,倘若是在不同时 ...
- Android 使用RxJava实现一个发布/订阅事件总线
1.简单介绍 1.1.发布/订阅事件主要用于网络请求的回调. 事件总线可以使Android各组件之间的通信变得简单,而且可以解耦. 其实RxJava实现事件总线和EventBus比较类似,他们都依据与 ...
- 事件总线EventBus
什么是事件总线管理? 将事件放到队列里,用于管理和分发: 保证应用的各个部分之间高效的通信及数据,事件分发: 模块间解耦: 什么是EventBus? EventBus是发布/订阅的事件总线.Event ...
随机推荐
- DOM基础(四)
每次写DOM的时候,就觉得好像没什么好写,因为涉及到知识点的方面的确不多,对于DOM来说,更多的还是练习为主.在练习的时候,最好能结合着js基础语法的知识点来学习.这样,在学习DOM的时候就不会那么枯 ...
- 图形化代码阅读工具——Scitools Understand
Scitools出品的Understand 2.0.用了很多年了,比Source Insight强大很多.以前的名字叫Understand for C/C++,Understand for Java, ...
- Vue学习之路---No.4(分享心得,欢迎批评指正)
这里说声抱歉,周末因为有其他事,没有更新博客,那么我们今天继续上周5的说. 老规矩,先回顾一下上一次的重点: 1.利用V-if和v-else来提到show()和hide(),同时要记住,v-else一 ...
- CSS限制字数,超出部份显示点点点...
最近项目中需要用CSS实现限制字数,超出部份显示点点点...,只需要一下代码即可: width:400px;/*要显示文字的宽度*/ text-overflow :ellipsis; /*让截断的文字 ...
- 因为本地没有配置 localhost 导致的 eclipse 的奇葩问题
因为电脑没有配置 127.0.0.1 localhost,已经碰到两次奇葩问题了. 问题一: 我的博文http://www.cnblogs.com/sonofelice/p/5143746.html中 ...
- IIS 部署ASP.Net, WebAPI, Restful API, PUT/DELETE 报405错解决办法, webapi method not allowed 405
WebDAV 是超文本传输协议 (HTTP) 的一组扩展,为 Internet 上计算机之间的编辑和文件管理提供了标准.利用这个协议用户可以通过Web进行远程的基本文件操作,如拷贝.移动.删除等.在I ...
- Linux学习之Vim使用
一 为何要学Vim 所有的Unix Like系统都有自带vi编辑器 一些软件的编辑接口会自动调起vi 作为vi的升级版,vim具有程序编辑功能,而且具有代码颜色高亮显示.辨别代码的正确性等功能 以上优 ...
- Http相关
1.http请求 http请求分为三部分:请求行,请求头,请求正文 1. 请求行 请求方式 GET POST 请求资源路径 协议版本 GET与POST请求区别? get只能传递1kb以下数据,P ...
- SaaS模式应用之多租户系统开发(单数据库多Schema设计)
SaaS是Software-as-a-Service(软件即服务)的简称,这边具体的解释不介绍. 多租户的系统可以应用这种模式的思想,将思想融入到系统的设计之中. 一.多租户的系统,目前在数据库存储上 ...
- ICC_lab总结——ICC_lab6:版图完成
ICC_workshop的最后一个实验了.在这次的实验中,由于我使用ICC的版本与workshop的lab不是同一个版本,因此在后面的实验过程不是很顺利,主要是在LVS的过程中,最后的LVS没有通过. ...