Giter Site home page Giter Site logo

popfisher / liveeventbus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jeremyliao/liveeventbus

0.0 0.0 0.0 2.5 MB

:mailbox_with_mail:EventBus for Android,消息总线,基于LiveData,具有生命周期感知能力,支持Sticky,支持AndroidX,支持跨进程,支持跨APP

License: Apache License 2.0

Java 100.00%

liveeventbus's Introduction

LiveEventBus

license version

LiveEventBus是一款Android消息总线,基于LiveData,具有生命周期感知能力,支持Sticky,支持AndroidX,支持跨进程,支持跨APP(最新版本1.6.1:new::tada::tada:)

logo

为什么要用LiveEventBus

生命周期感知
  • 消息随时订阅,自动取消订阅
  • 告别消息总线造成的内存泄漏
  • 告别生命周期造成的崩溃
范围全覆盖的消息总线解决方案
  • 进程内消息发送
  • App内,跨进程消息发送
  • App之间的消息发送
更多特性支持
  • 免配置直接使用,懒人最爱
  • 支持Sticky粘性消息
  • 支持AndroidX
  • 支持延迟发送
  • 观察者的多种接收模式(全生命周期/激活状态可接受消息)

常用消息总线对比

消息总线 延迟发送 有序接收消息 Sticky 生命周期感知 跨进程/APP 线程分发
EventBus
RxBus
LiveEventBus

想了解更多?请点击:全面了解Android消息总线

在工程中引用

Via Gradle:

implementation 'com.jeremyliao:live-event-bus:1.6.1'

For AndroidX:

implementation 'com.jeremyliao:live-event-bus-x:1.6.1'

使用方法

多种消息发送方式

进程内发送消息
  • post
LiveEventBus
	.get("key_name")
	.post(value);
App内发送消息,跨进程使用
  • postAcrossProcess
LiveEventBus
	.get("key_name")
	.postAcrossProcess(value);
App之间发送消息
  • postAcrossApp
LiveEventBus
	.get("key_name")
	.postAcrossApp(value);
进程内发送消息,延迟发送
  • postDelay
LiveEventBus
	.get("key_name")
	.postDelay(value, 1000);
进程内发送消息,有序发送
  • postOrderly
LiveEventBus
	.get("key_name")
	.postOrderly(value);

订阅消息

以生命周期感知模式订阅消息
  • observe

具有生命周期感知能力,LifecycleOwner销毁时自动取消订阅,不需要调用removeObserver

LiveEventBus
	.get("key_name", String.class)
	.observe(this, new Observer<String>() {
	    @Override
	    public void onChanged(@Nullable String s) {
	    }
	});
以Forever模式订阅和取消订阅消息
  • observeForever

Forever模式订阅消息,需要调用removeObserver取消订阅

LiveEventBus
	.get("key_name", String.class)
	.observeForever(observer);
取消订阅消息
  • removeObserver
LiveEventBus
	.get("key_name", String.class)
	.removeObserver(observer);
Sticky模式

支持在订阅消息的时候设置Sticky模式,这样订阅者可以接收到之前发送的消息。

  • observeSticky

以Sticky模式订阅消息,具有生命周期感知能力,LifecycleOwner销毁时自动取消订阅,不需要调用removeObserver

LiveEventBus
        .get("sticky_key", String.class)
        .observeSticky(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s){
            }
        });
  • observeStickyForever

Forever模式订阅消息,需要调用removeObserver取消订阅,Sticky模式

LiveEventBus
        .get("sticky_key", String.class)
        .observeStickyForever(observer);

配置

在Application.onCreate方法中配置:

LiveEventBus
        .config()
        ...
  • lifecycleObserverAlwaysActive 配置LifecycleObserver(如Activity)接收消息的模式(默认值true)

  • autoClear 配置在没有Observer关联的时候是否自动清除LiveEvent以释放内存(默认值false)

更多配置信息,请点击:LiveEventBus的配置

混淆规则

-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.** { *; }

遇到问题?

如果遇到了一些使用上的问题(如收不到消息等),在版本1.6.1+上可以使用控制台辅助类Console获取LiveEventBus的内部信息,详见::Console的使用。若问题不能解决,请提issue。

其他分支版本

  • 支持AndroidX
  • 同master版本一致
  • 经典实现版,整个实现就一个java文件
  • 只支持激活状态(Started)可以实时收到消息,非激活状态(Stoped)无法实时收到消息,需等到Activity重新变成激活状态,方可收到消息
  • 不支持跨进程通信
  • v2版,历史版本,已废弃
  • 为了解决非激活态不能实时收到消息的问题,采用修改LiveData源码的方式实现

文档

实现原理

质量

  • 编写了30个测试用例以确保LiveEventBus能够正常运行。
  • 具体测试用例参见LiveEventBusTest

版本

版本 功能
1.6.x 优化接口设计,优化实现逻辑,修复一些问题
1.5.x 优化接口设计,使用起来更简洁
1.4.x 简化对外暴露的接口,重构核心实现,支持前后台线程调用
1.3.x 支持跨进程、跨APP通信
1.2.x 支持接收消息的模式,支持AndroidX
1.1.x 修复了一些问题
1.0.x 初版,支持基本功能

主要功能提交记录

  1. 主要功能完成(Jul 11, 2018)
  2. 支持Sticky(Aug 8, 2018)
  3. 修复在后台线程PostValue会丢失消息的问题(Aug 9, 2018)
  4. 解决发送给Stop状态Observer消息无法及时收到的问题(Aug 18, 2018)
  5. 解决了Resumed状态的Activity发生订阅,订阅者会收到订阅之前发布的消息的问题。特别感谢@MelonWXD发现了这个问题(Dec 8,2018)
  6. 在removeObserver的时候,检查livedata上有没有observer,没有则删除这个livadata,以减少内存占用。特别感谢@GreenhairTurtle提供的解决方案(Dec 27,2018)
  7. 支持设置LifecycleObserver接收消息的模式,支持在整个生命周期实时接收消息和只在激活态实时接收消息两种模式(Jan 22,2019)
  8. 支持AndroidX(Mar 8,2019)
  9. 支持跨进程、跨APP(Mar 26,2019)
  10. 简化对外暴露的接口,重构核心实现,支持前后台线程调用(Apr 4,2019)

其他

  • 欢迎提Issue与作者交流
  • 欢迎提Pull request,帮助 fix bug,增加新的feature,让LiveEventBus变得更强大、更好用

More Open Source by JeremyLiao

  1. FastSharedPreferences 一个Android平台的高性能key-value组件
  2. tensorflow-lite-sdk 一个更加通用的Tensorflow-Lite Android SDK
  3. android-modular 一个组件化实施方案的Demo
  4. MessageBus 一个android平台的基于订阅-发布模式的消息框架,支持跨进程消息通信
  5. persistence 一个android平台的key-value storage framework
  6. LightRxAndroid 另辟蹊径,利用Android Handler实现了一个类似RxJava的链式框架
  7. rxjava-retry 封装了几个处理RxJava Retry操作的类
  8. retrofit-mock 一个用于Retrofit mock response数据的工具
  9. jacoco-android-demo AndroidStudio运行jacoco计算测试覆盖率的Demo
  10. android-gradle-study 深入浅出Android Gradle
  11. invoking-message 消息总线框架,基于LiveEventBus实现。它颠覆了传统消息总线定义和使用的方式,通过链式的方法调用发送和接收消息,使用更简单
  12. DataLoader 一个Android异步数据加载框架,用于Activity打开之前预加载数据,页面启动速度优化利器

liveeventbus's People

Contributors

jeremyliao avatar showmethe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.