Explain how to use Android aidl in detail

  • 2021-12-04 19:53:45
  • OfStack

AIDL is one of the IPC (Inter-Process Communication) modes in Android, and AIDL is the abbreviation of Android Interface definition language (for Xiaobai, the function of AIDL is to let you bind an service of other APP in your APP, so that your APP can interact with other APP.)

AIDL is only one of many inter-process communication modes in Android,

Differences between AIDL and Messenger:

Messenger is not suitable for a large number of concurrent requests: Messenger processes messages sent by clients in a serial way. If a large number of messages are sent to the server at the same time, the server can only process them one by one. Messenger is primarily for message delivery: This scenario does not apply to Messenger for methods that require cross-process calls to the server side. The bottom implementation of Messenger is AIDL, and the system is encapsulated for us to facilitate the call of the upper layer. AIDL is suitable for a large number of concurrent requests and cases involving server-side method calls

Principle of AIDL communication: First of all, look at this file and have a class called proxy. This is a proxy class, which runs in the client. In fact, the communication between processes realized by AIDL is not direct communication. Both the client and the server communicate through proxy: the method called by the client actually calls the method in proxy, and then proxy returns the returned result to the client through communication with the server.

1. The role of AIDL

AIDL is used for IPC communication of Android, so you can communicate within one APP or create two APP to communicate.

The functions of AIDL are clearly assigned, with Service running as a background as a server to manage various interactions, and Client as a method for clients to request data or call Service.

2. Simple use of AIDL

1) Create an aidl file, right-click directly to create it,

package com.example.mytest;


// IMyAidlInterface.aidl
package com.example.mytest;
 
// Declare any non-default types here with import statements
 
interface IMyAidlInterface {
  /**
   * Demonstrates some basic types that you can use as parameters
   * and return values in AIDL.
   */
  void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
      double aDouble, String aString);
  String add(int x , int y);
}

2) Select the. aidl file you just created to produce the corresponding java file.

AndroidStudio can be accomplished through Build-"model App

3) Write the concrete object implementation interface of Service


package com.example.mytest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class FirstService extends Service {
  public FirstService() {
  }
  private static String Tag = "FirstService";
  @Override
  public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    //throw new UnsupportedOperationException("Not yet implemented");
    Log.d(Tag,"service on bind");
    return mBinder;
  }
  @Override
  public void onCreate() {
    super.onCreate();
    Log.d(Tag,"OnCreate");
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(Tag,"onStartCommand");
    return START_STICKY;
  }
  @Override
  public boolean onUnbind(Intent intent) {
    Log.d(Tag,"onUnbind");
    return super.onUnbind(intent);
  }
  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(Tag,"onDestroy");
  }
  IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub(){
    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
    }
    @Override
    public String add(int x, int y) throws RemoteException {
      Log.d(Tag,x + "--" + y);
      return String.valueOf(x + y);
    }
  };
}

Note: onBund returns IBinder type, which will be transferred for later callbacks

4) Determine the content of Service in AndroidManifest. xml under 1


 <service
      android:name=".FirstService"
      android:enabled="true"
      android:exported="true">
      <intent-filter>
        <action android:name="com.example.mytest.aidl.FirstService"/>
      </intent-filter>
 
    </service>

5) Open the service


 private Button btnStartService; 
  private Button btnBindService;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    initView();
  }
 
  private void initView() {
    tvId = (TextView) findViewById(R.id.tv_id);
 
    btnStartService = (Button) findViewById(R.id.btn_Start_Service);
    btnStartService.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent intent = new Intent();
        intent.setPackage("com.example.mytest");
        intent.setAction("com.example.mytest.aidl.FirstService");
        startService(intent);
      }
    });
 
    btnBindService = (Button) findViewById(R.id.btnBindService);
    btnBindService.setOnClickListener(new View.OnClickListener() {
      @Override
        public void onClick(View view) {
            bind();
        }
    });
  
 
  private void bind(){
    Log.d(Tag, "bind");
    Intent intent = new Intent();
    intent.setPackage("com.example.mytest");
    if(controllerConnection != null){
      this.bindService(intent,controllerConnection,this.BIND_AUTO_CREATE);// Bind services and establish links 
    }
    else {
      Log.d(Tag, "controllerConnection != null");
    }
  }
 
  private void unbind(){
    if(controllerConnection != null && myAIDLController.asBinder().isBinderAlive()){
      try{
        Log.d(Tag, "this.unbindService(controllerConnection);");
        this.unbindService(controllerConnection);
      } catch (Exception localException) {
        Log.w(Tag, "unbind Exception localException");
      }
    }
  }

It is asynchronous at bind, so you can use onServiceConnected () to determine the operation after binding.


private ServiceConnection controllerConnection = new ServiceConnection(){
 
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
      Log.d(Tag,"onServiceConnected");
 
      myAIDLController = IMyAidlInterface.Stub.asInterface(iBinder);
      if (myAIDLController != null) {
        try {
          Log.d(Tag, "ServiceConnection success");
          Toast.makeText(MainActivity.this, "ServiceConnection success", Toast.LENGTH_LONG).show();
        } catch (Exception localException) {
          Log.w(Tag, "Exception localException");
        }
      }
    }
 
    @Override
    public void onServiceDisconnected(ComponentName componentName) {
      Log.d(Tag,"onServiceDisconnected");
      Toast.makeText(MainActivity.this, "onServiceDisconnected", Toast.LENGTH_LONG).show();
 
      myAIDLController = null;
    }
 
    @Override
    public void onBindingDied(ComponentName name) {
      Log.d(Tag,"onBindingDied");
    }
 
    @Override
    public void onNullBinding(ComponentName name) {
      Log.d(Tag,"onNullBinding");
    }
  };

6) Call the Service method add ()

When calling, you need to bind the Service method, which is already there. Next, it is simple to call. Create an Button, and then get the control object of Service, and call the method add


 btnServiceFunc = (Button) findViewById(R.id.btnServiceFunc);
    btnServiceFunc.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        try {
          Log.d(Tag, String.valueOf( myAIDLController.add(1,2)));
        } catch (RemoteException e) {
          e.printStackTrace();
        }
      }
    });

Related articles: