“This is the fourth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

AIDL briefly

Usually each application runs in its own process, but sometimes objects need to be passed between processes. You can write a service that runs in a different process through the application UI. On The Android platform, one process usually cannot access memory areas in other processes. So, they need to split objects into simple forms that the operating system can understand, so that they can masquerade as objects that can be accessed across borders. Writing this kind of camouflage code is pretty tedious, but Android provides an AIDL tool to do it.

AIDL stands for Android Interface Definition Language. AIDL is a description Language used to define the communication Interface between the server and the client, which can be used to generate code for IPC. AIDL is actually a template in a sense, because it’s not an AIDL file that actually works, it’s an instance of IInterface code that’s generated from it, and AIDL is a template that’s created to keep us from writing code again and again.

AIDL was designed to enable interprocess communication. In Android, each process runs in a separate block of memory where it performs its activities, separated from other processes. But sometimes we have a need to interact with each other, to compare and transfer data or delegate tasks, and AIDL was created to meet this need. With AIDL, you can retrieve data from one process and invoke exposed methods from another process to meet the needs of interprocess communication.

Generally, the application that exposes methods to call other applications is called the server, and the application that calls methods of other applications is called the client. The client interacts by binding the Service of the server.

AIDL is an IDL language that generates code that allows two processes running on an Android device to interact using an internal communication process. If you need to access methods of an object in one process (for example, in an Activity) in another process (for example, in a Service), you can use AIDL to generate code that camouflages passing various parameters.

To use AIDL, a Service needs to provide the Service interface in the form of an AIDL file. The AIDL tool will generate a corresponding Java interface and include a stub Service stub class for function calls in the generated Service interface. The Service implementation class needs to inherit from the stub Service stub class. The onBind method of the Service returns the object of the implementation class, which we can then use, as shown in the following example:

Start by creating an IMyRemoteService. Aidl file

package org.allin.android.remote;
interface IMusicControlService{
        void play(); 
        void stop(); 
        void pause();
}
Copy the code

Compile in AS, which generates a Java interface class from the AIDL file. The generated interface class will have an inner class, Stub class. All we need to do is to inherit the Stub class:

public class RemoteMusicService extends Service { private static final String TAG = "RemoteMusicService"; private MediaPlayer mediaPlayer; /* * (non-Javadoc) * * @see android.app.Service#onBind(android.content.Intent) */ @Override public IBinder onBind(Intent  intent) { return binder; } private final IMusicControlService.Stub binder = new IMusicControlService.Stub() { @Override public void stop() throws  RemoteException { Log.d(TAG,"stop...." ); if (mediaPlayer ! = null) { mediaPlayer.stop(); Try {// Mediaplayer.prepare () should be called before the start function is used to play the game again. } catch (IOException ex) { ex.printStackTrace(); } } } @Override public void play() throws RemoteException { Log.d(TAG,"play...." ); if (mediaPlayer == null) { mediaPlayer = MediaPlayer.create(RemoteMusicService.this, R.raw.tmp); mediaPlayer.setLooping(false); } if (! mediaPlayer.isPlaying()) { mediaPlayer.start(); } } @Override public void pause() throws RemoteException { Log.d(TAG,"pause...." ); if (mediaPlayer ! = null && mediaPlayer.isPlaying()) { mediaPlayer.pause(); }}}; @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); if(mediaPlayer ! = null){ mediaPlayer.stop(); mediaPlayer.release(); }}}Copy the code

When a client application connects to the Service, the onServiceConnected method is called, and the client gets the IBinder object. See the client onServiceConnected method below:

private ServiceConnection sc = new ServiceConnection() {
         
        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicService = null;
            Log.d(TAG, "in onServiceDisconnected");
        }
         
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicService = IMusicControlService.Stub.asInterface(service);
            Log.d(TAG, "in onServiceConnected");
        }
    };
Copy the code

This article is a brief introduction to Service cross-process communication using AIDL in Android. So far, the content of service is just the most basic use. The content of service is finished for now.

Next up is AIDL.