一漩勤、前言:
LiveEventBus是一款A(yù)ndroid消息總線(xiàn)感挥,基于LiveData,具有生命周期感知能力越败,支持Sticky触幼,支持AndroidX,支持跨進(jìn)程究飞,支持跨APP置谦。
為什么要用LiveEventBus?
1、生命周期感知
- 消息隨時(shí)訂閱亿傅,自動(dòng)取消訂閱
- 告別消息總線(xiàn)造成的內(nèi)存泄漏
- 告別生命周期造成的崩潰
2媒峡、范圍全覆蓋的消息總線(xiàn)解決方案
- 進(jìn)程內(nèi)消息發(fā)送
- App內(nèi),跨進(jìn)程消息發(fā)送
- App之間的消息發(fā)送
3葵擎、 更多特性支持
- 免配置直接使用谅阿,懶人最?lèi)?ài)
- 支持Sticky粘性消息
- 支持AndroidX
- 支持延遲發(fā)送
- 觀察者的多種接收模式(全生命周期/激活狀態(tài)可接受消息)
4、 常用消息總線(xiàn)對(duì)比
消息總線(xiàn) | 延遲發(fā)送 | 有序接收消息 | Sticky | 生命周期感知 | 跨進(jìn)程/APP | 線(xiàn)程分發(fā) |
---|---|---|---|---|---|---|
EventBus | ? | ? | ? | ? | ? | ? |
RxBus | ? | ? | ? | ? | ? | ? |
LiveEventBus | ? | ? | ? | ? | ? | ? |
想了解更多酬滤?請(qǐng)點(diǎn)擊:全面了解Android消息總線(xiàn)
demo下載:https://github.com/JeremyLiao/LiveEventBus.git
二奔穿、使用:
1、依賴(lài):
Via Gradle:
implementation 'com.jeremyliao:live-event-bus:1.7.2'
For AndroidX:
implementation 'com.jeremyliao:live-event-bus-x:1.7.2'
2敏晤、訂閱消息
- 以生命周期感知模式訂閱消息
LiveEventBus
.get("some_key", String.class)
.observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
}
});
- 以Forever模式訂閱消息
LiveEventBus
.get("some_key", String.class)
.observeForever(observer);
3、發(fā)送消息
- 不定義消息直接發(fā)送
LiveEventBus
.get("some_key")
.post(some_value);
- 先定義消息缅茉,再發(fā)送消息
//定義類(lèi)型
public class DemoEvent implements LiveEvent {
public final String content;
public DemoEvent(String content) {
this.content = content;
}
}
//發(fā)送消息
LiveEventBus
.get(DemoEvent.class)
.post(new DemoEvent("Hello world"));
4嘴脾、在Application.onCreate方法中配置:
LiveEventBus
.config()
.autoClear(true)
.lifecycleObserverAlwaysActive(true);
lifecycleObserverAlwaysActive
配置LifecycleObserver(如Activity)接收消息的模式(默認(rèn)值true):
true:整個(gè)生命周期(從onCreate到onDestroy)都可以實(shí)時(shí)收到消息
false:激活狀態(tài)(Started)可以實(shí)時(shí)收到消息,非激活狀態(tài)(Stoped)無(wú)法實(shí)時(shí)收到消息蔬墩,需等到Activity重新變成激活狀態(tài)译打,方可收到消息autoClear
配置在沒(méi)有Observer關(guān)聯(lián)的時(shí)候是否自動(dòng)清除LiveEvent以釋放內(nèi)存(默認(rèn)值false)setJsonConverter
配置JsonConverter(默認(rèn)使用gson)setLogger
配置Logger(默認(rèn)使用DefaultLogger)enableLogger
配置是否打印日志(默認(rèn)打印日志)setContext
如果廣播模式有問(wèn)題,請(qǐng)手動(dòng)傳入Context拇颅,需要在application onCreate中配置
三奏司、詳細(xì)使用文檔
1、 獲取Observable
- 通過(guò)name獲取Observable
Observable<T> get(@NonNull String key, @NonNull Class<T> type)
Observable<Object> get(@NonNull String key)
- 通過(guò)event type獲取Observable
<T extends LiveEvent> Observable<T> get(@NonNull Class<T> eventType)
2樟插、 消息發(fā)送
進(jìn)程內(nèi)發(fā)送消息
void post(T value)
App內(nèi)發(fā)送消息韵洋,跨進(jìn)程使用
void postAcrossProcess(T value)
App之間發(fā)送消息
void postAcrossApp(T value)
進(jìn)程內(nèi)發(fā)送消息,延遲發(fā)送
void postDelay(T value, long delay)
進(jìn)程內(nèi)發(fā)送消息黄锤,延遲發(fā)送搪缨,帶生命周期
void postDelay(LifecycleOwner sender, T value, long delay)
進(jìn)程內(nèi)發(fā)送消息,有序發(fā)送
void postOrderly(T value)
以廣播的形式發(fā)送一個(gè)消息
- 需要跨進(jìn)程鸵熟、跨APP發(fā)送消息的時(shí)候調(diào)用該方法
- 建議盡量使用postAcrossProcess副编、postAcrossApp
void broadcast(T value, boolean foreground, boolean onlyInApp)
3、 消息訂閱
以生命周期感知模式訂閱消息
- 具有生命周期感知能力流强,LifecycleOwner銷(xiāo)毀時(shí)自動(dòng)取消訂閱痹届,不需要調(diào)用removeObserver
void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer)
以Forever模式訂閱和取消訂閱消息
- Forever模式訂閱消息呻待,需要調(diào)用removeObserver取消訂閱
void observeForever(@NonNull Observer<T> observer)
取消訂閱消息
void removeObserver(@NonNull Observer<T> observer)
Sticky模式訂閱消息
- Sticky模式
- 支持在訂閱消息的時(shí)候設(shè)置Sticky模式,這樣訂閱者可以接收到之前發(fā)送的消息队腐。
- 以Sticky模式訂閱消息蚕捉,具有生命周期感知能力,LifecycleOwner銷(xiāo)毀時(shí)自動(dòng)取消訂閱香到,不需要調(diào)用removeObserver
void observeSticky(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer)
Sticky模式Forever訂閱消息
- Forever模式訂閱消息鱼冀,需要調(diào)用removeObserver取消訂閱,Sticky模式
void observeStickyForever(@NonNull Observer<T> observer)
4悠就、 跨進(jìn)程消息
支持對(duì)基本數(shù)據(jù)類(lèi)型消息的跨進(jìn)程發(fā)送
- int
- float
- long
- boolean
- double
- String
支持Serializable和Parcelable類(lèi)型消息的跨進(jìn)程發(fā)送
- 提供SerializableProcessor
- 提供ParcelableProcessor
支持Bean類(lèi)型消息的跨進(jìn)程發(fā)送
- 提供GsonProcessor以Gson方式提供支持
- 需要用注解@IpcConfig指定GsonProcessor:
@IpcConfig(processor = GsonProcessor.class)
支持自定義擴(kuò)展
- 實(shí)現(xiàn)自定義Processor慎式,實(shí)現(xiàn)Processor接口
- 用注解@IpcConfig指定自定義Processor
5、 配置
在Application.onCreate方法中配置:
LiveEventBus
.config()
...
lifecycleObserverAlwaysActive 配置LifecycleObserver(如Activity)接收消息的模式(默認(rèn)值true)
autoClear 配置在沒(méi)有Observer關(guān)聯(lián)的時(shí)候是否自動(dòng)清除LiveEvent以釋放內(nèi)存(默認(rèn)值false)
更多配置信息禁筏,請(qǐng)點(diǎn)擊:LiveEventBus的配置
6繁涂、混淆規(guī)則
-dontwarn com.jeremyliao.liveeventbus.**
-keep class com.jeremyliao.liveeventbus.** { *; }
-keep class android.arch.lifecycle.** { *; }
-keep class android.arch.core.** { *; }
for androidx:
-dontwarn com.jeremyliao.liveeventbus.**
-keep class com.jeremyliao.liveeventbus.** { *; }
-keep class androidx.lifecycle.** { *; }
-keep class androidx.arch.core.** { *; }