EventBus第一章:初识,相见,了解,使用

19年了,距离上次写文章都不知道多久了,该认真起来了,这次记录的是我Learn EventBus的使用以及理解。

初识Eventbus

用官方的话说:EventBus is a publish/subscribe event bus for Android and Java(EventBus是用于Android和Java中的发布/订阅事件总线).下面是我从Github上copy的一张图,很形象的说明了他的工作原理,这里如果看不懂也没什么问题,后面我会一一道来。

首先看一下为什么使用EventBus,它的优点在什么地方:

1.简化你的代码。Android中使用广播用于组件之间的通信(只说应用内),监听app内部的消息,注册接收者,发送者等,是比较繁琐的,而EventBus帮我们简化了这些代码,更加容易去使用。
2.更快。(这里为什么更快,等到下一篇会解开这个秘密)。
3.体积更小。仅仅只有不到50K。

EventBus的使用在Gradle中

implementation ‘org.greenrobot:eventbus:3.1.1’

EventBus的使用在我们的代码中

在需要进行接收事件的Activity或者Fragment,或者其他类中,我们首先需要进行注册

EventBus.getDefault().register(this);

以及解绑

EventBus.getDefault().unregister(this);

在这推荐当类创建的时候注册EventBus,当类销毁的时候进行解绑,例如在Activity的onCreate方法中进行注册,在onDestroy方法中进行解绑。
当我们需要发送事件时EventBus为我们提供了发送事件的两种方法,分别是:

EventBus.getDefault().post(new TestEvent("hello Eventbus"));

注意这里的TestEvent是我们随便写的一个类,在实际项目中要根据你需要传递的数据来自定义类,该类又被称为订阅参数,EventBus就是根据该订阅参数来区分某一个订阅方法(指的是接受事件的方法)是否接受该发送的事件(后面解析源码的时候我会说到这一块)。另外一种是发送粘性事件,指的是该发送的事件如果不被删除,将会存在于整个应用的生命周期中,除非调用方法删除该事件,当某一个粘性订阅方法能够接收该事件时,该订阅方法就会被触发,接收粘性事件:

EventBus.getDefault().postSticky(new TestEvent("hello sticky Eventbus"));

订阅类中订阅方法需要注意的几点这里进行说明:

@Subscribe(threadMode = ThreadMode.MAIN,sticky = true,priority = 100)    public void onTest(TestEvent event) {        textView.setText(event.msg);    }

1.必须使用Subscribe注解进行声明,否则无法接收事件。
2.订阅方法必须设置为public,否则无法接收事件,具体为什么在分析源码时我会说到。
3.订阅方法返回类型和方法名并没有过多约束,但建议统一为void onEvent。

在上面的订阅方法中可以看到注解后面带上了三个参数,分别为threadMode,sticky,priority。分别代表线程模式,粘性事件,以及优先级。在具体实现时,可以根据项目要求选择任意或者不选任何参数。接下来讲一下每个参数的含义。

ThreadMode

ThreadMode是一个枚举类型,包含POSTING,MAIN,MAIN_ORDERED,BACKGROUND,ASYNC。个人习惯一般使用的是MAIN,当然这要根据你的具体需求。这里借用一下官方的解释(翻译不到之处,敬请谅解):

1.POSTING
Subscriber will be called directly in the same thread, which is posting the event. This is the default.
意思是订阅着会被唤醒在发送事件的线程中,默认就是该线程模式。

2.MAIN
On Android, subscriber will be called in Android’s main thread (UI thread). If the posting thread is the main thread, subscriber methods will be called directly, blocking the posting thread. Otherwise the event is queued for delivery (non-blocking)
意思是订阅者会被唤醒在Android主线程也就是UI线程,如果是在主线程发送事件,订阅方法会被立刻调用,阻塞发送线。反之该事件会被加入队列中(通过Handler机制来进行时间的分发,不阻塞主线程。感兴趣的可以去了解一下Handler为什么内部不会阻塞主线程)。

3.MAIN_ORDERED
On Android, subscriber will be called in Android’s main thread (UI thread). Different from {@link #MAIN}, the event will always be queued for delivery. This ensures that the post call is non-blocking.
意思是该订阅者将会被唤醒在主线程,不同于MAIN模式的是,该发送事件首先会被加入队列,这是确保post事件不会阻塞线程。

4.BACKGROUND
On Android, subscriber will be called in a background thread. If posting thread is not the main thread, subscriber methods will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single background thread
意思是订阅者会被唤醒在后台线程,如果post不在主线程,订阅方法会被立即调用。如果post在主线程,则EventBus使用一个单独的后台线程来调用订阅方法(内部使用线程池)。

5.ASYNC
Subscriber will be called in a separate thread
这句话可能比较好理解就是订阅者会在单独的线程中调用(内部依旧使用线程池),通过名字就可以看出它是非同步的

Sticky

声明该订阅方法为可以接受粘性订阅事件。默认为false。
注意如果之前存在该订阅参数类型的粘性事件,且未被移除,则该订阅方法仍然可以接收之前的事件。
之前的粘性事件只会在该订阅方法被订阅的时候才会读取一次,并不会多次调用。

Priority

声明订阅方法接受事件的优先级,相同订阅参数类型的订阅方法按照优先级高低来排队,不声明默认为0,

这篇文章先写到这里,这篇属于使用篇,下一篇我会继续分析EventBus的源码,以及具体的实现,这篇文章还留下两个疑问分别是;EventBus为什么更快,以及为什么订阅方法必须是public类型的,下一篇文章我会解答,期待下次再见。

更多相关文章

  1. android 情景模式之响铃+震动获取方法
  2. Android(安卓)WebView 软键盘遮挡输入框问题的解决方法
  3. LayoutInflater的inflate方法使用
  4. android6.0源码分析之AMS服务源码分析
  5. Ubuntu Linux14 64位下在Android(安卓)studio下用gradle编译Andr
  6. Android(安卓)Http协议访问网络
  7. Android使用Service播放音乐
  8. Android(安卓)开发常见问题
  9. Android(安卓)屏蔽系统通知的几种方法

随机推荐

  1. android 多线程 AsyncTask handler
  2. Android最全UI库合集
  3. 2020年 Android必会面试题
  4. android上opengl性能分析工具
  5. Android自动化测试初探(三): 架构实现
  6. Android(安卓)Binder机制学习总结(二)-Dr
  7. Android修改触摸按键震动强弱
  8. Android(安卓)动画——过渡动画中Activit
  9. IT网站收藏
  10. Android(安卓)Intent 用法全面总结