AIDL introduction

IPC stands for Inter-Process Communication. AIDL (Android Interface Definition Language) is one of them. It has powerful functions and supports one-to-many concurrent Communication and real-time Communication. AIDL allows you to bind a service from another APP (or process) to your APP so that your APP can interact with other apps.

AIDL use

① Data class preparation

AIDL deals with data communication between processes, so there must be data first. Create book.java and implement the Parcelable interface.

package com.zhxumao.android.aidl;
import android.os.Parcel;
import android.os.Parcelable;
public class Book implements Parcelable {
    int bookId;
    String bookName;
    protected Book(Parcel in) {
        bookId = in.readInt();
        bookName = in.readString();
    }
    public Book(int bookId, String bookName) {
        this.bookId = bookId;
        this.bookName = bookName;
    }
    public static final Creator<Book> CREATOR = new Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel in) {
            return new Book(in);
        }
        @Override
        public Book[] newArray(int size) {
            return newBook[size]; }};@Override
    public int describeContents(a) {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) { dest.writeInt(bookId); dest.writeString(bookName); }}Copy the code

② Create an AIDL interface

1. Create the AIDL interface class iBookManager.aidl and add custom methods. Ibookmanager. aidl uses a custom Parcelable object Book (other than the 6 base types), so it needs to create a file with the same name Book. The implementation class for IBookManager can be found in the Build > Gen directory, which is automatically generated by the editor.

// IBookManager.aidl
package com.zhxumao.android.aidl;
import com.zhxumao.android.aidl.Book;
interface IBookManager {
    void addBook(in Book book);
    List<Book> getBookList(a);
}
Copy the code
// Book.aidl
package com.zhxumao.android.aidl;
// Declare any non-default types here with import statements
// Declare the parcelable object
parcelable Book;
Copy the code

The IBookManager implementation class generated by the editor focuses on the abstract static inner class Stub, which requires us to inherit the implementation in the Service.

package com.zhxumao.android.aidl;
public interface IBookManager extends android.os.IInterface
{
  /** Default implementation for IBookManager. */
  public static class Default implements com.zhxumao.android.aidl.IBookManager {
    // code omitted. }public static abstract class Stub extends android.os.Binder implements com.zhxumao.android.aidl.IBookManager {
    // code omitted. }public void addBook(com.zhxumao.android.aidl.Book book) throws android.os.RemoteException;
  public java.util.List<com.zhxumao.android.aidl.Book> getBookList() throws android.os.RemoteException;
}
Copy the code

③ Remote server Service implementation

Create bookManagerService. Java and inherit from Service 2. Bind to iBookManagerservice. Binder mBinder = new IBookManager.Stub(); Bind(); bind (); bind (); 4. In the method body defined by IBookManager, write your own server-side logic and server-side functionality.

public class BookManagerService extends Service {
    private CopyOnWriteArrayList<Book> mBookList = new CopyOnWriteArrayList<>();
    private Binder mBinder = new IBookManager.Stub() {
        @Override
        public void addBook(Book book) throws RemoteException {
            mBookList.add(book);
        }
        @Override
        public List<Book> getBookList(a) throws RemoteException {
            returnmBookList; }};@Nullable
    @Override
    public IBinder onBind(Intent intent) {
        returnmBinder; }}Copy the code
// Don't forget to declare the service in manifest.xml<service
    android:name=".BookManagerService"
    android:process=":aidl" />
Copy the code

④ Client implementation

Let’s say we’re in an Activity: 1. Implement the ServiceConnection interface class, and get the IBookManager object from onServiceConnected().

private IBookManager manager;
private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        manager = IBookManager.Stub.asInterface(service);
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {}};Copy the code

2. Start BookManagerService, use the bindService() binding to start the service, and set up ServiceConnection.

bindService(new Intent(MainActivity.this, BookManagerService.class), connection, Context.BIND_AUTO_CREATE);
Copy the code

After the service is started, we will get an instance of IBookManager in ServiceConnection. Only then can we write client logic, such as inserting/retrieving data into the service:

// Insert a piece of data
manager.addBook(new Book(1."android"));
// Get all the saved data of the service
List<Book> bookList = manager.getBookList();
Copy the code

Now that the entire AIDL usage is implemented, note that when you finally exit, if the service is no longer needed, don’t forget to learn how to bind the service.

@Override
protected void onDestroy(a) {
    super.onDestroy();
    unbindService(connection);
}
Copy the code

In conclusion, using AIDL for interprocess communication, the following steps are probably required:

  • Create the.aidl interface file.
  • Implement stubs for the AIDL interface in the Service and supplement the service business logic (note the thread concurrency).
  • The client implements ServiceConnection, and obtains the aiDL interface implementation class instance, and supplements the client business logic.