在上篇文章中,了解了ITest.java与Proxy,Stub三者的层次关系。
public interface ITest extends android.os.IInterface
{
//Stub是一个Binder,也是一个ITest
public static abstract class Stub extends android.os.Binder implements com.example.aidl.ITest
{
...
//Proxy是一个ITest
private static class Proxy implements com.example.aidl.ITest
{
...
}
...
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
//aidl中定义的方法
public int add(int a, int b) throws android.os.RemoteException;
}
Proxy调用add方法时,会调用Binder的transact方法,开始其跨进程的调用。
private static class Proxy implements com.example.aidl.ITest
{
private android.os.IBinder mRemote;
//Proxy必然会持有Binder实例
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public int add(int a, int b) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(a);
_data.writeInt(b);
//跨进程调用开始
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
transact方法会传入Stub.TRANSACTION_add这个标识,在Stub中的onTransact中处理:
public static abstract class Stub extends android.os.Binder implements com.example.aidl.ITest
{
private static final java.lang.String DESCRIPTOR = "com.example.aidl.ITest";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.aidl.ITest interface,
* generating a proxy if needed.
*/
public static com.example.aidl.ITest asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.aidl.ITest))) {
return ((com.example.aidl.ITest)iin);
}
return new com.example.aidl.ITest.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
java.lang.String descriptor = DESCRIPTOR;
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(descriptor);
return true;
}
//在这里处理add
case TRANSACTION_add:
{
data.enforceInterface(descriptor);
int _arg0;
_arg0 = data.readInt();
int _arg1;
_arg1 = data.readInt();
//注意,Stub中的add方法并没有实现
int _result = this.add(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
}
...
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
AIDL到底是用来干什么的呢?在这个例子中,定义了ITest.aidl后,生成了一个接口ITest.java,其中包含有Proxy和Stub,Proxy中的add方法,通过binder调用其他进程实现,也就是调用Stub中的add方法来实现。
所以,在Client端,只要能持有Proxy的引用,而Server端只要实现Stub接口,这样,Client便可调用Server中的方法了,前提是这些方法都在接口(ITest.aidl)中注册过。