EventBus(發(fā)布訂閱事件總線):通過解耦發(fā)布者和訂閱者簡化android 事件傳遞
EventBus is a publish/subscribe event bus for Android and Java.
開源庫地址:https://github.com/greenrobot/EventBus
一、EventBus 簡介
1.簡化了不同組件間(Activity,Fragment,Service) and 異步線程(Background Thread)與UI主線程間的通信
2.傳統(tǒng)的事件(消息)傳遞方式有:Intent、接口回調(diào)、Handler、BroadcastReceiver ,
相比之下 EventBus 的優(yōu)點:是代碼簡潔,使用簡單,解耦事件的發(fā)布和訂閱,更快,代碼更小,50K 左右的 jar 包
3.結(jié)構(gòu)關(guān)系圖:EventBus 負(fù)責(zé)存儲訂閱者、事件相關(guān)信息,訂閱者和發(fā)布者都只和 EventBus 關(guān)聯(lián),實現(xiàn)了事件發(fā)布和訂閱的解耦
Events are posted ({@link #post(Object)}) to the bus, which delivers it to subscribers that have a matching handler method for the event type.

4.事件響應(yīng)流程圖:
訂閱者首先調(diào)用 EventBus 的 register 接口訂閱某種類型的事件,當(dāng)發(fā)布者通過 post 接口發(fā)布該類型的事件時,EventBus 執(zhí)行訂閱者的事件響應(yīng)函數(shù)。

二、基本概念:
1.事件(Event):其實就是一個Object對象,可以是String 類型,可以是一個自定義的Event對象,事件的分類:普通事件和黏性事件,EventType:表示事件所屬的類
2.發(fā)布者(Publisher):發(fā)布某事件的對象 ,post(Object),postSticky(Object)
3.訂閱者(Subscriber):訂閱某種事件類型的對象,在事件響應(yīng)函數(shù)中對事件進行處理
Event handling methods must be annotated by {@link Subscribe}, must be public, return nothing (void), and have exactly one parameter (the event).
三、如何使用
How to get started with EventBus in 3 steps
1.添加依賴庫? compile'org.greenrobot:eventbus:3.1.1'
2.定義事件類型

3.聲明訂閱者事件響應(yīng)方法:
Event handling methods must be annotated by {@link Subscribe}, must be public, return nothing (void), and have exactly one parameter
事件響應(yīng)方法:必須:使用注解@Subscribe 聲明 ,必須是public ,返回類型是void ,只有一個參數(shù),Note that with EventBus 3 the method name can be chosen freely(EventBus3以后 方法名可以自定義)

4.注冊、解注冊事件: In Android, in activities and fragments you should usually register according to their life cycle.
Only while subscribers are registered, they will receive events.

5.Post events: Post an event from any part of your code. All currently registered subscribers matching the event type will receive it. 所有與事件類型相匹配的當(dāng)前已注冊的訂閱者都可以收到事件
EventBus.getDefault().post(new MessageEvent("Hello everyone!"));
四、源碼解析:
1. ThreadMode: http://greenrobot.org/eventbus/documentation/delivery-threads-threadmode/
線程模型:ThreadMode類是枚舉類,在訂閱者事件響應(yīng)方法中,可以通過注解的方式設(shè)置線程模型,EventBus內(nèi)置了4中線程模型,分別是ThreadMode.POSTING 、ThreadMode.MAIN、ThreadMode.BACKGROUND、ThreadMode.ASYNC
public enum ThreadMode {
? ? POSTING,(默認(rèn)的模式)
? ? MAIN,
? ? MAIN_ORDERED,
? ? BACKGROUND,
? ? ASYNC
}
(1). POSTING:事件在哪個線程發(fā)布出來的,事件處理函數(shù)就會在這個線程中運行——收發(fā)在同一個線程(由于發(fā)布事件方法可能運行在主線程,這意味著接收方法中不能執(zhí)行耗時操作,否則會阻塞主線程)
(2).MAIN:不論事件是在哪個線程中發(fā)布出來的,該事件處理函數(shù)都會在UI線程中立即執(zhí)行(這意味著接收事件方法不能執(zhí)行耗時操作,否則會阻塞主線程;同時,由于是「立即」調(diào)用,所以發(fā)射事件方法此時是會被接收事件方法所阻塞的)
(3).MAIN_ORDERED: 接收事件方法會被放入 MessageQueue 中等待執(zhí)行(這意味著發(fā)射事件方法是不會被阻塞的)
(4).BACKGROUND:如果事件是在UI線程中發(fā)布出來的,那么該事件處理函數(shù)就會在新的線程中運行,如果事件本來就是子線程中發(fā)布出來的,那么該事件處理函數(shù)直接在發(fā)布事件的線程中執(zhí)行(不能執(zhí)行UI更新操作)
(5).ASYNC:無論事件在哪個線程發(fā)布,該事件處理函數(shù)都會在新建的子線程中執(zhí)行(不能執(zhí)行UI更新操作)
2.黏性(Sticky )事件:當(dāng)事件發(fā)布后,再有訂閱者訂閱該類型事件,依然能收到該類型事件最近一個 Sticky 事件。
3.Subscribe
線程模式,黏性事件,線程優(yōu)先級
4.EventBus
register
unRegister
post
5.EventBusBuilder
參考文獻:
http://a.codekk.com/detail/Android/Trinea/EventBus%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90