AIDL中的Proxy,Stub以及其關係

在上篇文章中,瞭解了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)中註冊過。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章