当前位置:网站首页>Pleasant and burden free cross process communication

Pleasant and burden free cross process communication

2022-06-11 23:51:00 I'm Wong Tai Sin

1. background

A recent requirement involves cross process communication , The common practice of cross process communication is to define AIDL Interface , Then start a service , Get the binding service binder object , Then you can start communicating .

With more and more business modules , Each module needs to define its own AIDL Interface , This may lead to more and more interfaces , More and more difficult to manage .

Is there any elegant way to communicate across processes ?

I think of it. EventBus. At the beginning of learning Android When , This framework is used , I thought it was really easy to use , It can realize the decoupling between modules very gracefully .

Is it possible to refer to EventBus , Implement a cross process version of EventBus Well ? I've been referring to these days EventBus Design idea , Rolled up a framework ProcessBus, It is very convenient to realize cross process communication .
Welcome to build together .

Address :https://github.com/bearhuang-omg/ProcessBus

2. Usage mode

The interface provided is very simple :

Interface Parameters Return value remarks
init( Not necessary )context:Context nothing initialization , Pass in context Used for binding Service, If no incoming , Then, the components will be internally reflected context, Then bind Service
registercmd:String // Listening instructions
block: (Event)→Unit // Callback method for receiving instructions
Releasable // Pass in lifecycle You can automatically de register , Avoid memory leaks Listen for an instruction
unRegisterobserverKey:String // The only one returned during registration key nothing Anti registration
postevent:Event // Events sent nothing Send an event

Example :

// Monitoring events , And automatically de register 
Bus.register("testCmd2") {
     event ->
    Log.i(TAG, " Received the incident ")
}?.autoRelease(lifecycle)

// Send events 
Bus.post(Event("testCmd2", " The message sent "))

// Manually de register 
val key = Bus.register("testCmd1") {
     event ->
    Log.i(TAG, " Received the incident  ")
}?.key!!

Bus.unRegister(key)

As can be seen from the example ,ProcessBus It's very simple to use , And it can realize decoupling between different modules , Replace most of AIDL Interface .

ProcessBus Has the following characteristics :

  1. Simple interface ;
  2. There is no need to actively call the initialization method , Can be used anywhere ( Actively calling initialization methods can avoid reflection , Be efficient );
  3. Event Can carry accessories , For transferring large files .

3. The basic principle

chart

ProcessBus The structure diagram of is as follows :
 Insert picture description here
sdk Internally, a service will be started in the main process , Designed for cross process communication , When a child process registers or sends a message , Will automatically bind the service ,

After binding the service , At this time, the subprocess will replace the current ProcessKey and ICallBack Pass to the main process , among ProcessKey Is a string that uniquely marks a process ,ICallBack It's a binder object , The main process gets this binder After object , You can send messages to the child process .

technological process

The specific flow chart is as follows :
 Insert picture description here

step remarks
1bindService The child process is bound to the main process service , Get the... That communicates with the main process binder
2bind The child process passes through the binder take ProcessKey and ICallBack Pass to the main process , After the main process receives it, it saves it in map among
3reigster Subprocesses will focus on cmd Tell the main process , After the main process receives , Save in map among
4post Other child processes send a Event To main process , The main process will match after receiving cmd, Then get the corresponding one ICallBack
5ICallBack The main process gets the corresponding ICallBack after , perform ICallBack Method , take Event To the corresponding child process

About Event

Event The data structure of is as follows :

data remarks
cmd:String( must ) Instructions , The main process will send the command to the listening object
content:String ( must ) The content carried by the event
fromProcess:String ( sdk Inside filling ) Sent process name , from sdk Inside filling
attachmentBinder:IBinder ( Not necessary ) The attachment , It's a binder object , Suitable for transferring large files , The target process passes directly through binder Get attachments

4. summary

At present, only the most basic functions have been realized , You can consider adding comments later , Give Way sdk More convenient to use .

Welcome to build together .

原网站

版权声明
本文为[I'm Wong Tai Sin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112343175229.html