• android -------- LiveDataBus的使用


    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<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

  • 相关阅读:
    vue项目实战:原生百度地图api的使用
    vue项目实战:页面公共组件的全局注册动态引入的考虑
    celery+django的定时任务设置
    celery.exceptions.NotRegistered: 'tasks.add' + celery delay 坑 + python manage.py check 查看状态
    from __future__ import absolute_import的作用
    Django与celery集成:异步任务原理和过程
    python -m pip install --upgrade pip --default-timeout=100 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
    Django 的安全
    django集成sentry实现日志上报和钉钉告警
    TypeScript(21): 装饰器
  • 原文地址:https://www.cnblogs.com/zhangqie/p/10907474.html
Copyright © 2020-2023  润新知