likes
comments
collection
share

进程间的通信——AIDL的使用

作者站长头像
站长
· 阅读数 4

前言

本篇文章主要是讲述AIDL是如何使用的,而不会太具体地介绍一些代码上面的细节。具体的代码细节可以参考IPC机制之Binder机制这篇文章。我们设定一个情景,客户端向服务端添加书籍并获取当前书籍列表的信息,我们来看一下应该怎么实现。

服务端的创建

使用AIDL,也就是在Binder的基础上面进行进程间的通信。既然是双端通信,必须有一个客户端,一个服务端。这里我们创建一个服务端:

public class BookService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

服务端需要返回一个Binder对象给客户端,我们需要创建一个Binder对象:

private Binder binder = new IBookManager.Stub() {

        @Override
        public void addBook(Book book) throws RemoteException {
            if (book != null) {
                mBookList.add(book);
            }
        }

        @Override
        public List<Book> getBookList() throws RemoteException {
            return mBookList;
        }
 };

创建Binder对象需要一个Stub类继承了Binder类,而这里的IBookManager需要使用AIDL文件生成。

AIDL文件

  • 首先必须要有IBookManager.aidl文件,其中需要有addBook(Book book),getBookList()两个接口方法。
// IBookManager.aidl
package com.example.runningh.myapplication.aidl;

// Declare any non-default types here with import statements
import com.example.runningh.myapplication.aidl.Book;

interface IBookManager {
    void addBook(in Book book);
    List<Book> getBookList();
}

这里需要注意的是在AIDL中除了基本数据类型,其他类型的参数必须标明方向:in, out或者intout,in表示输入型参数,out表示输出型参数,inout表示输入输出型参数,我们需要根据实际需要去指定参数类型,不能一概使用out或者inout,因为这在底层实现是有开销的。而且在AIDL接口中只支持方法,不支持声明静态常量,这一点区别于传统的接口。

  • 还有一点也需要注意的是,AIDL中每个实现了Parcelable接口的类都需要按照上面那种方式去创建相应的AIDL文件并声明那个类为parcelable。所以我们还需要有一个Book.aidl(因为后面我们需要创建Book.java类)。
// Book.aidl
package com.example.runningh.myapplication.aidl;

// Declare any non-default types here with import statements
import com.example.runningh.myapplication.aidl.Book;

parcelable Book;

创建Book.java文件并生成IBookManager.java

创建Book类并实现Parcelable接口。

package com.example.runningh.myapplication.aidl;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by RunningH on 2017/12/10.
 */
public class Book implements Parcelable {
    public int bookId;
    public String bookName;

    public Book(int bookId, String bookName) {
        this.bookId = bookId;
        this.bookName = bookName;
    }

    protected Book(Parcel in) {
        bookId = in.readInt();
        bookName = in.readString();
    }

    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 new Book[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(bookId);
        dest.writeString(bookName);
    }
}

当aidl文件创建完毕后,通过Build—>make module project生成IBookManager.java文件。

客户端的创建

客户端我们使用BookManagerActivity。这里使用的是bindService的方式和服务端进行绑定起来。

package com.example.runningh.myapplication.aidl;

import android.app.Activity;
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.support.annotation.Nullable;
import android.util.Log;

import com.example.runningh.myapplication.R;

import java.util.List;

/**
 * Created by RunningH on 2017/12/10.
 */

public class BookManagerActivity extends Activity {
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IBookManager iBookManager = IBookManager.Stub.asInterface(service);
            try {
                List<Book> bookList = iBookManager.getBookList();
                Log.i("ABC", "query book list:" + bookList.toString());
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, BookService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }
}

Manifest的注册

最后我们需要在Manifest中注册上述我们创建的服务端,并将其运行在remote进程中。

<service
   android:name=".aidl.BookService"
   android:process=".remote"/>

结果

最后在Logcat中看到结果如下: query book list:[com.example.runningh.myapplication.aidl.Book@267a8b7, com.example.runningh.myapplication.aidl.Book@14c824]