鸿蒙开源组件——基于鸿蒙ActiveData实现的通信组件

jacksky
发布于 2021-11-29 17:10
浏览
0收藏

ActiveDatabus-ohos

介绍

基于鸿蒙ActiveData实现的通信组件,支持带有生命周期、粘性、以及线程分发的通信库; 同时将鸿蒙中的公共通信分发功能聚合到了该组件库中

软件架构

软件架构说明

安装教程

  1. 在工程的build.gradle文件中添加
    allprojects { repositories { ... maven { url 'https://jitpack.io' } } }​
  2. 在model的build.gradle中添加

    功能说明

    1.发送消息

    2.发送延时消息

    3.发送生命周期感知消息,只有在传入的生命周期初始化以后才会发送,否则取消发送

    4.发送广播消息

    5.支持消息回调跟随Ability生命周期,slice生命周期,内置生命周期(需手动移除观察者)

    6.消息回调线程配置,包括主线程,后台线程与默认线程

    7.支持黏性消息,先发送再注册消息回调,之前发送的消息可以收到

    8.应用处于后台时是否接收消息配置

 

使用说明

1.发送消息

LiveEventBus
	.get("key")
	.post(value);

2.发送延时消息

ActiveDataBus.get("key").postDelay(value, delay);

3.发送生命周期感知消息

ActiveDataBus.get("key").post(lifeCycleOwner, value);

4.发送广播消息

 IntentParams intentParams = new IntentParams();
            intentParams.setParam("test", "hello");
new BroadcastPublishHelper.EventPublishBuild(new Intent.OperationBuilder().withAction("action").build())
            .withIntentParams(intentParams).build().publishEvent();

5.消息回调配置ability生命周期或slice生命周期

 ActiveDataBus.get("key",value.class).observe(lifeCycleOwner, value -> {
            });

6.消息回调配置内置生命周期与移除观察者

Observer<value> observer = value -> {}
ActiveDataBus.get("key",value.class).observeForever(observer);
ActiveDataBus.get("key",value.class).removeObserver(observer);

7.消息回调线程配置

ActiveDataBus.get(mainTest, String.class).withThreadMode(ThreadMode.MAIN).observe(this, s -> {
        });

8.消息回调到后台线程配置线程池

ActiveDataBus.config().setThreadPoolExecutor(Executors.newFixedThreadPool(3));

9.消息回调黏性配置

 ActiveDataBus.get("key",value.class).withSticky(true/false).observe(this, t -> {

            });

10.配置应用处于后台时是否可以收到消息

ActiveDataBus.get(alwaysTest).withBackground(true).observe(this, t -> {

        });

11.广播消息回调

 BroadCastManager.getInstance().registerCommonEvent(o -> {
            }, "action", lifeCycleOwner);

注意事项

1.广播消息的黏性配置设置为true以后不能更改,更改也不生效

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

 

 

active-databus-ohos-master.zip 103.27K 3次下载
已于2021-11-29 17:10:28修改
收藏
回复
举报
回复
    相关推荐