这是GitHub地址,在我写这篇博客之前EventBus已经更新到3.2了,我们学习使用的是3.0版。EventBus库中最重要的三个点,分别是subscriber(订阅者),事件(消息),publisher(发布者)。
subscriber ——> EventBus 的register方法,传入的Activity实例进行注册,指明该Activity可以接受EventBus发布的事件
事件(Event)——> EventBus 的post方法,发布的事件,
publisher(发布者)——> EventBus的post方法。可以让注册了的Activity收到发布的消息
第一步:添加依赖库
implementation 'org.greenrobot:eventbus:3.0.0'
第二步:注册与注销
这里按照官方提供的方法,在onStart()注册,在onStop()注销,如下
@Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { super.onStop(); EventBus.getDefault().unregister(this); }
第三步:简单使用
首先我们先创建一个消息类型的类
public class FirstEvent {
private String mMsg;
public FirstEvent(String msg) {
mMsg = msg;
}
public String getMsg(){
return mMsg;
}
}
发送消息时
EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));
接收消息时,3.0版就与之前的版本不同了,前面的版本采用
onEvent()
onEventMainThread()
onEventBackgroundThread()
onEventAsync()
来实现事件的接收,但是3.0版本后采用订阅事件@Subscribe(threadMode = ThreadMode.订阅模式),订阅的模式查看源码的注释
public enum ThreadMode {
/**
* Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery
* implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for
* simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers
* using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.
*/
POSTING,
/**
* Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is
* the main thread, event handler methods will be called directly. Event handlers using this mode must return
* quickly to avoid blocking the main thread.
*/
MAIN,
/**
* Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods
* will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single
* background thread, that will deliver all its events sequentially. Event handlers using this mode should try to
* return quickly to avoid blocking the background thread.
*/
BACKGROUND,
/**
* Event handler methods are called in a separate thread. This is always independent from the posting thread and the
* main thread. Posting events never wait for event handler methods using this mode. Event handler methods should
* use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number
* of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus
* uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
*/
ASYNC
}
这里的POSTING、MAIN、BACKGROUND、ASYNC与之前版本的onEvent()、onEventMainThread()、onEventBackgroundThread()、onEventAsync()是一一对应的。
ThreadMode.POSTING 订阅者方法将在发布事件所在的线程中被调用。这是 默认的线程模式。事件的传递是同步的,一旦发布事件,所有该模式的订阅者方法都将被调用。这种线程模式意味着最少的性能开销,因为它避免了线程的切换。因此,对于不要求是主线程并且耗时很短的简单任务推荐使用该模式。使用该模式的订阅者方法应该快速返回,以避免阻塞发布事件的线程,这可能是主线程。
ThreadMode.MAIN 订阅者方法将在主线程(UI线程)中被调用。因此,可以在该模式的订阅者方法中直接更新UI界面。如果发布事件的线程是主线程,那么该模式的订阅者方法将被直接调用。使用该模式的订阅者方法必须快速返回,以避免阻塞主线程。
ThreadMode.BACKGROUND 订阅者方法将在后台线程中被调用。如果发布事件的线程不是主线程,那么订阅者方法将直接在该线程中被调用。如果发布事件的线程是主线程,那么将使用一个单独的后台线程,该线程将按顺序发送所有的事件。使用该模式的订阅者方法应该快速返回,以避免阻塞后台线程。
ThreadMode.ASYNC 订阅者方法将在一个单独的线程中被调用。因此,发布事件的调用将立即返回。如果订阅者方法的执行需要一些时间,例如网络访问,那么就应该使用该模式。避免触发大量的长时间运行的订阅者方法,以限制并发线程的数量。EventBus使用了一个线程池来有效地重用已经完成调用订阅者方法的线程。
所以如果我们这个事件处理需要更新UI那么我们可以使用
//这里我们采用的是ThreadMode.MAIN订阅模式,根据入参对象event处理不同的事件
@Subscribe(threadMode = ThreadMode.MainThread)
public void onEventMainThread(FirstEvent event) {
……
}
