LiveData是17年GoogleIO大会上提出来的一个新技术。相对于通信总线类型的框架EventBus和RxBus来说,它更简单,更简洁、更解耦。


LiveEventBus是一款Android消息总线,基于LiveData,具有生命周期感知能力,支持Sticky,支持AndroidX,支持跨进程,支持跨APP


LiveDataBus优点

LiveDataBus的实现及其简单

相对EventBus复杂的实现,LiveDataBus只需要一个类就可以实现


LiveDataBus可以减小APK包的大小

LiveDataBus只依赖Android官方组件LiveData,本身实现只一个类。EventBus 57Kb、RxJava 2.2M


LiveDataBus 依赖方支持更好

LiveDataBus只依赖Android官方组件LiveData,相比RxBus依赖的RxJava和RxAndroid,依赖方支持更好


LiveDataBus具有生命周期感知


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() {@Overridepublic void onChanged(@Nullable String s) {Log.i("aaa",s);}});

发起通信

LiveEventBus.get().with("LiveDataBusDemo1").post("LiveDataBus1");

以Forever模式订阅和取消订阅消息,Forever模式订阅消息,需要手动调用removeObserver取消订阅

注册

private Observer observer = new Observer() {@Overridepublic 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() {@Overridepublic void onChanged(@Nullable String s) {Log.i("aaa",s);textView.setText("observeSticky注册的观察者收到消息: " + s);}});

发起通信(你可以在注册之前和注册之后分辨发起通知看效果)

LiveEventBus.get().with("LiveDataBusDemo3").post("LiveDataBus3");

observeStickyForever,Forever模式订阅消息,需要手动调用removeObserver取消订阅,和上面的一样


注册

private Observer observer = new Observer() {@Overridepublic 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

 

更多相关文章

  1. Android跨进程通信——Activity
  2. Android HTTP 实现与服务器通信
  3. android音乐播放器Service的生命周期分析
  4. Android官方入门文档[18]与其他碎片通信
  5. Android基于XMPP Smack Openfire开发IM【三】客户端接收服务器发
  6. android 消息提醒(Toast,Notification)
  7. Android Messenger IPC 通信
  8. Android与PC的socket通信
  9. android蓝牙开发入门到精通4------通信标准

随机推荐

  1. Android:调用系统分享功能
  2. Android(安卓)LCD
  3. android notification 的例子
  4. Android自定义对话框(Dialog)位置,大小
  5. Android学习之Activity生命周期文档翻译
  6. Android(安卓)Shadow 阴影制作[转]
  7. Android(安卓)code cut 1 --圆形在屏幕撞
  8. android openGL简例1
  9. android 获得当前活动的Acticity
  10. Android(安卓)studio3.1.2 坑——3rd-par