当前位置:网站首页>愉快无负担的跨进程通信方式
愉快无负担的跨进程通信方式
2022-06-11 23:44:00 【我是黄大仙】
1.背景
最近做的一个需求涉及到跨进程通信,跨进程通信通常的做法就是定义 AIDL 接口,然后开启一个服务,绑定服务拿到 binder 对象,接着就可以开始通信了。
随着业务模块越来越多,每个模块都需要定义自己的 AIDL 接口,这样可能会导致接口越来越多,越来越难以管理。
有没有什么比较优雅的跨进程通信的方式呢?
我想到了 EventBus。在刚开始学习 Android 的时候,就用到了这个框架,当时觉得真的非常好用,能够非常优雅的实现模块之间的解耦。
那是不是可以参考 EventBus ,实现一个跨进程版本的 EventBus 呢?这几天参考了 EventBus的设计思想,撸了一个框架 ProcessBus,可以很方便的实现跨进程的通信。
欢迎一起共建。
地址:https://github.com/bearhuang-omg/ProcessBus
2.使用方式
提供的接口非常简单:
| 接口 | 参数 | 返回值 | 备注 |
|---|---|---|---|
| init(非必须) | context:Context | 无 | 初始化,传入 context 用于绑定 Service,如果没有传入,则组件内部会通过反射拿到context,然后绑定 Service |
| register | cmd:String //监听的指令 block: (Event)→Unit //收到指令的回调方法 | Releasable // 传入 lifecycle 可以自动反注册,避免内存泄漏 | 监听某个指令 |
| unRegister | observerKey:String //注册的时候返回的唯一 key | 无 | 反注册 |
| post | event:Event //发送的事件 | 无 | 发送某个事件 |
例子:
//监听事件,并自动反注册
Bus.register("testCmd2") {
event ->
Log.i(TAG, "收到了事件")
}?.autoRelease(lifecycle)
//发送事件
Bus.post(Event("testCmd2", "发出来的消息"))
//手动反注册
val key = Bus.register("testCmd1") {
event ->
Log.i(TAG, "收到了事件 ")
}?.key!!
Bus.unRegister(key)
从例子当中可以看出,ProcessBus 使用起来非常简单,并且能够实现不同模块之间的解耦,替代大多数的AIDL接口。
ProcessBus 有以下特点:
- 简洁的接口;
- 无需主动调用初始化方法,可以随处使用(主动调用初始化方法可以避免反射,效率要高);
- Event 可以携带附件,用于传输大文件。
3.基本原理
结构图
ProcessBus 的结构图如下所示:
sdk 内部会在主进程开启一个服务,专门用于跨进程通信,子进程在注册或者发消息时,都会自动的去绑定这个服务,
完成了服务的绑定之后,子进程这个时候就会将当前的 ProcessKey 和 ICallBack 传递给主进程,其中 ProcessKey 是能够唯一标记进程的字符串,ICallBack 是一个 binder 对象,主进程拿到了这个 binder 对象之后,就可以给子进程发送消息了。
流程
具体的流程图如下所示:
| 步骤 | 备注 | |
|---|---|---|
| 1 | bindService | 子进程绑定主进程服务,获取到和主进程通信的binder |
| 2 | bind | 子进程通过主进程的 binder 将 ProcessKey 和 ICallBack 传递给主进程,主进程收到之后将其保存在 map 当中 |
| 3 | reigster | 子进程将自己关注的 cmd 告诉主进程,主进程收到之后,保存在 map 当中 |
| 4 | post | 其他的子进程发送一个 Event 给主进程,主进程收到之后匹配 cmd,然后拿到对应的 ICallBack |
| 5 | ICallBack | 主进程拿到对应的 ICallBack 之后,执行 ICallBack 方法,将 Event 抛给对应子进程 |
关于 Event
Event 的数据结构如下所示:
| 数据 | 备注 |
|---|---|
| cmd:String(必须) | 指令,主进程会根据指令发送给监听的对象 |
| content:String (必须) | 事件携带的内容 |
| fromProcess:String ( sdk 内部填充) | 发送的进程名,由 sdk 内部填充 |
| attachmentBinder:IBinder (非必须) | 附件,是一个 binder 对象,适用于传输大文件,目标进程直接通过 binder 获取到附件 |
4.总结
目前只实现了最基础的功能,之后可以考虑增加注解,让 sdk 使用起来更加方便。
欢迎一起共建。
边栏推荐
- HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
- Meet o & M (I) common questions for O & M interview
- RF中使用reuqests的两种方式
- Lake Shore—SuperVariTemp 低温恒温器
- How to construct PostgreSQL error codes
- oracle中dblink操作
- Share a treasure website necessary for new media operation for free
- Two way leading circular linked list (C language)
- Solr之基础讲解入门
- SAP SD 创建/修改价格表
猜你喜欢

2022 safety officer-b certificate theoretical question bank and simulation test

Lake Shore—SuperTran-VP 连续流低温恒温器系统

Anaconda download package error: valueerror: check_ hostname requires server_ hostname

Cisco private dynamic routing protocol: EIGRP

Lake Shore - supervaritemp low temperature thermostat

(dp+ group backpack) acwing 9 Group knapsack problem

2022年安全員-A證考題模擬考試平臺操作

(simple statistics) acwing 3404 Who are your potential friends

Pad printing process flow and application precautions

Solr之基础讲解入门
随机推荐
Altium designer工程下多个原理图和PCB图的一一对应
Figure overview of neural network
Teach you to play with SSM framework
New Year Countdown JS case
What is webstorage? And cookies
Wake up wrist - neural network and deep learning (tensorflow application) updating
Integrate工具之Jenkins
JS to add an attribute to an array object
(linear DP | monotone queue) acwing 895 Longest ascending subsequence
HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
Lake Shore - supertran VP continuous flow cryogenic thermostat system
一文读懂Logstash原理
Here we go! Dragon lizard community enters PKU classroom
On the knowledge points of cookie attributes and the differences between webstorage and cookies?
帝国理工等最新《胶囊网络综述》论文,29页pdf阐述胶囊的概念、方法与应用
Introduction to Solr Basics
2022年R1快开门式压力容器操作考题及在线模拟考试
机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】
2022年安全員-A證考題模擬考試平臺操作
队列(C语言)