Android uses Messenger for Service IPC communication analysis, androidipc
If you want to perform IPC communication, write an AIDL interface and a Service subclass. Then, implement the AIDL interface and return it to the Activity interface layer as an IBinder.
If you do not want to write the AIDL interface file, but want to communicate with the Service in a single I thread, we can use the Messenger class written in Android to process the file. Similarly, we can pass messages to the Service for communication.
Write the basic code first:
Public class MyService extends Service {Messenger messenger = null; public MyService () {}@ Overridepublic void onCreate () {super. onCreate (); messenger = new Messenger (handler); // instantiate an object and bind the Handler, ui mq message detection} Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {Log. d ("", "Msg:" + msg. what + ";" + msg. obj) ;}};/* (non-Javadoc) * @ see android. app. service # onBind (android. content. intent) * // @ Overridepublic IBinder onBind (Intent arg0) {return messenger. getBinder (); // return the Binder interface }}
Some code in MyActivity
<Span style = "white-space: pre"> </span> Messenger messenger; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bindService (new Intent (this, MyService. class), conn, Context. BIND_AUTO_CREATE); // bind a Connection} private ServiceConnection conn = new ServiceConnection () {@ Overridepublic void onServiceDisconnected (ComponentNa Me cn) {}@ Overridepublic void onServiceConnected (ComponentName cn, IBinder ibiner) {messenger = new Messenger (ibiner); // get MessengerLog in Service. d ("", "OK messenger connected ........ ") ;}}; public void sendMsg (int what, String msg) {// This method is to transmit your own business information to the Service for Handler processing. If (messenger = null) {return;} Message msg1 = Message. obtain (); msg1.what = what; msg1.obj = msg; try {messenger. send (msg1);} catch (RemoteException e) {e. printStackTrace ();}}
The above code is very simple.
Let's take a look at the Messenger source code is actually a packaging class, which itself is the object that can be delivered by IPC, because the Parcelable interface is used.
Public Messenger (Handler target) {// The Handler previously passed here, mTarget = target. getIMessenger (); // The IMessenger interface in Handler} public void send (Message message) throws RemoteException {mTarget. send (message); // specific imesinder interface sending} public IBinder getBinder () {return mTarget. asBinder ();} // I have removed all comments
Let's take a look at how the Handler source code sends messages.
Final IMessenger getIMessenger () {synchronized (mQueue) {if (mMessenger! = Null) {return mmesimp;} mMessenger = new MessengerImpl (); // This implements the IMessenger remote IPC interface, AIDL return mMessenger;} private final class MessengerImpl extends IMessenger. stub {// implement public void send (Message msg) {Handler. this. sendMessage (msg); // basically Handler is used to send messages }}
We can see that the remote communication IPC interface IMessenger contains the send (Message msg) method. In essence, it still obtains the Handler object in MyService to send messages.
IMessenger.aidl
The content is as follows:
package android.os;import android.os.Message;/** @hide */oneway interface IMessenger { void send(in Message msg);}
The above source code is the source code of the new version of Version 4.1. This part should be consistent. Below is a simple sketch, which is a summary.
Steps required for creating a service on android
Let's take a look at the binder. If we create a service, we should use inter-process communication. If we cannot figure out the binder principle, it is basically impossible to establish a service, or at least we should study a service that android has or is written by someone else, and then imitate it to write it, including the classes required to create ipc communication.
What is the communication (IPC) between android processes?
Android is based on the Linux kernel. Therefore, linux supports IPC and android. Such as Named Pipes and shared memory. In addition, android uses its own unique IPC binder, which is mainly used for remote calls between two processes. However, this involves how to remotely call the API to PASS Parameters and return results. This requires the caller to package and package the data, which is a tedious process. Therefore, android introduces aidl (android interface description launguage). The developer defines aidl. android will produce the stub code according to the description of aidl to help the caller package and unpack the data. What developers need to do Is inherit the stub code and implement functions in the stub code. These functions are defined in aidl.