What is AIDL

AIDL is one of the inter-process Communication (IPC) modes in Android. AIDL is the abbreviation of Android Interface Definition Language. The purpose of AIDL is to allow you to bind a service from another APP to your APP, so that your APP can interact with other apps.

Ii. Use of AIDL

2.1 Code writing of AIDL Server

The directory structure is as follows:



Among them:

  • Imyaidlinterface. aidl represents the AIDL interface that we define
  • User.aidl represents the custom type we transfer
  • MainActivity represents the main page
  • MyService represents a custom service that returns an instance of IMyAidlInterface.Stub
  • User represents the specific custom type we’re transferring, unlike User.aidl, which only defines references

IMyAidlInterface.aidl

// IMyAidlInterface.aidl
package com.example.aidlserver;

import com.example.aidlserver.User;
// 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.
     */
    String getName();
    User getUserName(String data);
}
Copy the code

User.aidl

// User.aidl
package com.example.aidlserver;

parcelable User;

Copy the code

MyService

package com.example.aidlserver; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import androidx.annotation.Nullable; public class MyService extends Service { private static final String TAG = "MyService"; private static User user; // public MyService() { // } @Override public void onCreate() { super.onCreate(); user = new User("zhangsan"); } @Nullable @Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind" + "" ); return new MyBinder(); } static class MyBinder extends IMyAidlInterface.Stub { @Override public String getName() throws RemoteException { return "test"; } @Override public User getUserName(String data) throws RemoteException { return user; }}}Copy the code

User

package com.example.aidlserver; import android.os.Parcel; import android.os.Parcelable; public class User implements Parcelable { public User(String name) { this.name = name; } public User(Parcel in) { name = in.readString(); } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel in) { return new User(in); } @Override public User[] newArray(int size) { return new User[size]; }}; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); }}Copy the code

AndroidManifest.xml

<? The XML version = "1.0" encoding = "utf-8"? > <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aidlserver"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" android:exported="true" android:enabled="true"> </service> </application> </manifest>Copy the code

2.2 AIDL client code writing

The directory code structure of the client is as follows:

Aidl and User. Aidl must be the same as those on the server. Therefore, copy the aiDL folder on the server to the client

The User needs to be the same as the Server and the package name needs to be the same, so create a new aidlServer package under com.example and copy the User code of the Server side in this package

MainActivity code

package com.example.aidlclient; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.View; import androidx.appcompat.app.AppCompatActivity; import com.example.aidlserver.IMyAidlInterface; public class MainActivity extends AppCompatActivity { private IMyAidlInterface iMyAidlInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindService(); } ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName  name, IBinder service) { iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { serviceConnection = null; }}; private void bindService() { Intent intent = new Intent(); intent.setClassName("com.example.aidlserver", "com.example.aidlserver.MyService"); bindService(intent, serviceConnection, BIND_AUTO_CREATE); } private static final String TAG = "MainActivity"; public void onClick(View view) { try { Log.d(TAG, "onClick" + ", iMyAidlInterface = " + iMyAidlInterface.getName() + ", iMyAidlInterface.getUserName(1) = " + iMyAidlInterface.getUserName("1") + "" + ""); } catch (RemoteException e) { e.printStackTrace(); }}}Copy the code

You can view the print logs on the console

Git code

See AIDL in Android for details