安卓的AIDL总结

时间:2022-11-28 22:35:45

安卓中跨进程通讯时常常需要AIDL,因此总结一下AIDL的用法和简要分析AIDL的工作流程。

在Android Studio中可以直接创建AIDL文件,创建的AIDL文件会生成在新创建的aidl文件夹下。

建议在aidl下建立实体类,这样可以将整个aidl直接拷贝,建立的文件列表如下:
安卓的AIDL总结
Book是实现了Parcelable的实体类:

public class Book implements Parcelable{
public String usrName;
public int usrId;
public boolean isMale;

public Book(String usrName, boolean isMale, int usrId) {
this.usrName = usrName;
this.isMale = isMale;
this.usrId = usrId;
}

protected Book(Parcel in) {
usrName = in.readString();
usrId = in.readInt();
isMale = in.readByte() != 0;
}

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.writeString(usrName);
dest.writeInt(usrId);
dest.writeByte((byte) (isMale ? 1 : 0));
}
}

定义Book.aidl使得IBookManager.aidl可以找到实体类:

// Book.aidl
package com.example.aidl;

parcelable Book;

记得在IBookManager.aidl 中import com.example.aidl.Book

// IBookManager.aidl
package com.example.aidl;

// Declare any non-default types here with import statements
//需要导入实体类
import com.example.aidl.Book;
interface IBookManager {
/**
* 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);
//定义两个需要实现的方法
List<Book> getBookList();
void addBook(in Book book);
}

为了能够编译aidl文件夹下的Book.java,需要在应用的build.gradle中做如下配置:

sourceSets{
main{
java.srcDirs = ['src/main/java', 'src/main/aidl']
}
}

接下来就可以进行进程间通信了,我们可以给Service指定android:process=":remote"来模拟进程间通信。
在Activity中绑定Service:

        Intent intent = new Intent(this, MainService.class);
mConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
mIBookManager = IBookManager.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
bindService(intent, mConn, Context.BIND_AUTO_CREATE);

在Service的onBind方法中返回Binder对象:

public class MainService extends Service {
ArrayList<Book> mBookList;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBookManager();
}

@Override
public void onCreate() {
super.onCreate();
mBookList=new ArrayList<Book>();
}
class MyBookManager extends IBookManager.Stub{

@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {}

@Override
//实现定义的接口方法,用于被跨进程调用
public List<com.example.aidl.Book> getBookList() throws RemoteException
{
return mBookList;
}

@Override
//实现定义的接口方法,用于被跨进程调用
public void addBook(com.example.aidl.Book book) throws RemoteException
{
mBookList.add(book);
}
}
}

根据IBookManager.Stub.asInterface(service)获取IBookManager对象,这样就可以在Activity中利用mIBookManager对象跨进程调用Service的方法了,例如在Activity中:

public void addBook(View view) {
if (mIBookManager != null) {
try {
//将会调用Service中MyBookManager实例的addBook方法。
mIBookManager.addBook(new Book("momo", false, 0));
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

接着分析一下根据IBookManager.aidl自动生成的IBookManager.java文件。

package com.example.aidl;

public interface IBookManager extends android.os.IInterface {
/**
* Local-side IPC implementation stub class.
*/

public static abstract class Stub extends android.os.Binder implements com.example.aidl.IBookManager {...}

/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/

public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException;

public java.util.List<com.example.aidl.Book> getBookList() throws android.os.RemoteException;

public void addBook(com.example.aidl.Book book) throws android.os.RemoteException;
}

可以看出IBookManager接口extends android.os.IInterface,包含两个我们定义在IBookManager.aidl的待实现的方法,同时内部包含了一个静态内部抽象类Stub,Stub既继承自Binder又实现了IBookManager,IBookManager.Stub就是我们需要创建的进行通信的Binder对象。
接着看一下Stub的简要结构:

public static abstract class Stub extends Binder implements IBookManager {
private static final java.lang.String DESCRIPTOR = "com.example.aidl.IBookManager";

/**
* Construct the stub at attach it to the interface.
*/

public Stub() {
this.attachInterface(this, DESCRIPTOR);
}

/**
* Cast an IBinder object into an com.example.aidl.IBookManager interface,
* generating a proxy if needed.
*/

public static IBookManager asInterface(IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.example.aidl.IBookManager))) {
return ((com.example.aidl.IBookManager) iin);
}
return new com.example.aidl.IBookManager.Stub.Proxy(obj);
}

@Override
public android.os.IBinder asBinder() {
return this;
}

@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_basicTypes: {
...
return true;
}
case TRANSACTION_getBookList: {
...
return true;
}
case TRANSACTION_addBook: {
...
return true;
}
}
return super.onTransact(code, data, reply, flags);
}

private static class Proxy implements IBookManager {
private android.os.IBinder mRemote;

Proxy(android.os.IBinder remote) {
mRemote = remote;
}

@Override
public android.os.IBinder asBinder() {
return mRemote;
}

public java.lang.String getInterfaceDescriptor() {
return DESCRIPTOR;
}

/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/

@Override
public void basicTypes(...) throws RemoteException {
...
try {
...
mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);
_reply.readException();
} finally {
...
}
}

@Override
public java.util.List<Book> getBookList() throws RemoteException {
...
try {
...
mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
...
} finally {
...
}
return _result;
}

@Override
public void addBook(Book book) throws RemoteException {
...
try {
...
mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);
} finally {
...
}
}
}

static final int TRANSACTION_basicTypes = (FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getBookList= (FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_addBook = (FIRST_CALL_TRANSACTION + 2);
}

可以看到Stub内部包含了一个实现了IBookManager的静态内部代理类Proxy ,Stub.asInterface(Ibinder binder)返回的就是Proxy实例,代理类在实现方法内最终都是调用了mRemote.transact()方法。transact()方法第一个参数code是int类型值,代表的是具体调用的方法序号,服务端根据onTransact()根据code值来判断调用的方法。
客户端执行transact方法后,当前线程会被挂起,直到服务端onTransact执行并返回数据后,当前线程继续执行

//FIRST_CALL_TRANSACTION 的值为1
static final int TRANSACTION_basicTypes = (FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getBookList= (FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_addBook = (FIRST_CALL_TRANSACTION + 2);

@Override
public boolean onTransact(int code, ...) throws RemoteException {
switch (code) {
...
case TRANSACTION_basicTypes: {
...
return true;
}
case TRANSACTION_getBookList: {
...
return true;
}
case TRANSACTION_addBook: {
...
return true;
}
}
return super.onTransact(code, data, reply, flags);
}

可以看出Stub内部把方法从1开始排号,服务端根据该code的值来判断调用的方法,并没有传递真正的函数名。

参考自《Android开发艺术探索》