android cross-process communication

 

        Android has also read a lot about the knowledge of process communication, but I still feel that I can't fully understand it. Now I write down my own understanding, and then make corrections and supplements later.

       

        1 What is aidl?

         The android interface definition language is used for IPC (internal inter-process communication). With aidl, we can define the communication interface between processes.

        2 Why use aidl?

         The official answer given by android is: AIDL is only used when you allow APPs from different clients to access your service and need to deal with multi-threading issues. If you don't consider concurrency, you can use Binder, if you don't consider multithreading, you can use Messenger.

        3 How to use aidl?

           1) The server defines the aidl interface file (AidlService)

           2) Define the service (RemoteService), implement the method in AidlService.Stub, and return AidlService.Stub in the onBind method

           3) The client also adds the aidl file, starts the service through bindservice (intent, conn, Context.BIND.AUTO.CREATE), obtains the Ibinder object in onServiceConnected of conn, and then obtains the proxy of AidlService through AidlService.Stub.asInterface(ibinder) Class that implements calls to the RemoteService method.

         4, the principle of aidl

           1) The defined aidl file ( AidlService.aidl ), after clean/rebuild, will generate a .java file with the same name in the gen directory. The code is mainly composed of two parts: proxy class Proxy and abstract class Stub. Stub inherits the Binder class, implements the AidlService interface, and Proxy implements the AidlService interface.

           2) Return the implementation of AidlService.Stub in onBind of RemoteService  .

           3) The client obtains the Proxy class through AidlService.Stub.asInterface(iBinder). When calling the method through the proxy, it will call the onTrancact method of the stub of the remote server by calling the trancact method of the iBinder. onTrancate calls different methods with parameters and returns.

 

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361411&siteId=291194637