# ActiveDatabus-ohos **Repository Path**: talkwebyunchaung/active-databus-ohos ## Basic Information - **Project Name**: ActiveDatabus-ohos - **Description**: 基于鸿蒙ActiveData实现的通信组件,同时将鸿蒙中的公共通信分发功能聚合到了该组件库中 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 15 - **Forks**: 0 - **Created**: 2021-06-09 - **Last Updated**: 2022-09-06 ## Categories & Tags **Categories**: harmonyos-communication **Tags**: None ## README # ActiveDatabus-ohos #### 介绍 基于鸿蒙ActiveData实现的通信组件,支持带有生命周期、粘性、以及线程分发的通信库; 同时将鸿蒙中的公共通信分发功能聚合到了该组件库中 #### 软件架构 软件架构说明 #### 安装教程 1. 在工程的build.gradle文件中添加 ``` allprojects { repositories { ... maven { url 'https://jitpack.io' } } } ``` 2. 在model的build.gradle中添加 ``` dependencies { implementation 'com.gitee.talkwebyunchaung:active-databus-ohos' } ``` #### 功能说明 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 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](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)