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)中注册过。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值