LiveData是17年GoogleIO大会上提出来的一个新技术。相对于通讯总线类型的框架EventBus和RxBus来讲,它更简单,更简洁、更解耦。android
LiveEventBus是一款Android消息总线,基于LiveData,具备生命周期感知能力,支持Sticky,支持AndroidX,支持跨进程,支持跨APPgit
LiveDataBus优势github
LiveDataBus的实现及其简单 框架
相对EventBus复杂的实现,LiveDataBus只须要一个类就能够实现ide
LiveDataBus能够减少APK包的大小post
LiveDataBus只依赖Android官方组件LiveData,自己实现只一个类。EventBus 57Kb、RxJava 2.2Mthis
LiveDataBus 依赖方支持更好spa
LiveDataBus只依赖Android官方组件LiveData,相比RxBus依赖的RxJava和RxAndroid,依赖方支持更好code
LiveDataBus具备生命周期感知server
LiveDataBus具备生命周期感知,在Android系统中使用调用者不须要调用反注册,相比EventBus和RxBus使用更为方便,而且没有内存泄漏风险。
LiveEventBus的特色
生命周期感知,消息随时订阅,自动取消订阅
支持Sticky粘性消息
支持AndroidX
支持跨进程通讯
支持跨APP通讯
支持设置LifecycleObserver(如Activity)接收消息的模式:
1.整个生命周期(从onCreate到onDestroy)均可以实时收到消息
2.激活状态(Started)能够实时收到消息,非激活状态(Stoped)没法实时收到消息,需等到Activity从新变成激活状态,方可收到消息
在工程中引用
implementation 'com.jeremyliao:live-event-bus:1.4.2'
配置
在 Application.onCreate 方法中配置:
LiveEventBus.get() .config() .supportBroadcast(this) .lifecycleObserverAlwaysActive(true);
具备生命周期感知能力,LifecycleOwner销毁时自动取消订阅,不须要调用removeObserver
保证 with() 中的key值相同,(注册/接收)和发起通讯
注册:
LiveEventBus.get().with("LiveDataBusDemo1",String.class).observe(this, new Observer<String>() { @Override public void onChanged(@Nullable String s) { Log.i("aaa",s); } });
发起通讯
LiveEventBus.get().with("LiveDataBusDemo1").post("LiveDataBus1");
以Forever模式订阅和取消订阅消息,Forever模式订阅消息,须要手动调用removeObserver取消订阅
注册
private Observer<String> observer = new Observer<String>() { @Override public void onChanged(@Nullable String s) { Log.i("aaa",s); textView.setText("observeForever注册的观察者收到消息: " + s); } }; LiveEventBus.get() .with("LiveDataBusDemo2", String.class) .observeForever(observer);
发起通讯
LiveEventBus.get().with("LiveDataBusDemo2").post("LiveDataBus2");
手动取消订阅消息
LiveEventBus.get() .with("LiveDataBusDemo2", String.class) .removeObserver(observer);
Sticky模式(能够接收 前面/未注册以前 发起通讯的信息)
支持在订阅消息的时候设置Sticky模式,这样订阅者能够接收到以前发送的消息,固然也支持后面发送的信息
以Sticky模式订阅消息,具备生命周期感知能力,LifecycleOwner销毁时自动取消订阅,不须要调用removeObserver
注册
LiveEventBus.get() .with("LiveDataBusDemo3", String.class) .observeSticky(this, new Observer<String>() { @Override public void onChanged(@Nullable String s) { Log.i("aaa",s); textView.setText("observeSticky注册的观察者收到消息: " + s); } });
发起通讯(你能够在注册以前和注册以后分辨发起通知看效果)
LiveEventBus.get().with("LiveDataBusDemo3").post("LiveDataBus3");
observeStickyForever,Forever模式订阅消息,须要手动调用removeObserver取消订阅,和上面的同样
注册
private Observer<String> observer = new Observer<String>() { @Override public void onChanged(@Nullable String s) { Log.i("aaa",s); textView.setText("observeStickyForever注册的观察者收到消息: " + s); } }; LiveEventBus.get() .with("LiveDataBusDemo4", String.class) .observeStickyForever(observer);
发起通讯
LiveEventBus.get().with("LiveDataBusDemo4").post("LiveDataBus4");
手动取消订阅消息
LiveEventBus.get() .with("LiveDataBusDemo4", String.class) .removeObserver(observer);
混淆规则
-dontwarn com.jeremyliao.liveeventbus.** -keep class com.jeremyliao.liveeventbus.** { *; } -keep class android.arch.lifecycle.** { *; } -keep class android.arch.core.** { *; }
参考文档:https://github.com/JeremyLiao/LiveEventBus