取到
? ? ? ? ? ? if (((iin != null) && (iin instanceof com.cqumonk.calculate.aidl.ICalculateAIDL))) {
? ? ? ? ? ? ? ? return ((com.cqumonk.calculate.aidl.ICalculateAIDL) iin);? //如果得到的实例是ICalculateAIDL的对象,则返回
? ? ? ? ? ? }
? ? ? ? ? ? return new com.cqumonk.calculate.aidl.ICalculateAIDL.Stub.Proxy(obj);//如果无法得到本地实现的对象则会返回一个代理对象
}
? ? ? ? 在这个方法中,首先在系统中查找注册的的service,如果没有找到,那么一定是别的apk实现的service,于是返回一个此service的静态代理类对象供Client调用。我们来看一下这个代理,创建时我们将clientBinder传入了,同时也它实现了ICalculateAIDL接口:
private static class Proxy implements com.cqumonk.calculate.aidl.ICalculateAIDL {
? ? ? ? ? ? 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;
? ? ? ? ? ? }
? ? ? ? ? ? @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);? //调用binderDriver的提供的方法将参数发给服务端
? ? ? ? ? ? ? ? ? ? _reply.readException();
? ? ? ? ? ? ? ? ? ? _result = _reply.readInt();? //读取到返回结果
? ? ? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? ? ? _reply.recycle();
? ? ? ? ? ? ? ? ? ? _data.recycle();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return _result;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public int minus(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_minus, _data, _reply, 0);
? ? ? ? ? ? ? ? ? ? _reply.readException();
? ? ? ? ? ? ? ? ? ? _result = _reply.readInt();
? ? ? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? ? ? _reply.recycle();
? ? ? ? ? ? ? ? ? ? _data.recycle();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return _result;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? 代理中也实现了ICalculateAIDL接口定义的方法,我们以add方法为例,里面将参数打包发送给Server端。在Server端收到请求后,会调用service中我们实现的那个stub对象(mBinder)的onTransact方法:
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
? ? ? ? ? ? switch (code) {
? ? ? ? ? ? ? ? case INTERFACE_TRANSACTION: {
? ? ? ? ? ? ? ? ? ? reply.writeString(DESCRIPTOR);
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case TRANSACTION_add: {
? ? ? ? ? ? ? ? ? ? data.enforceInterface(DESCRIPTOR);
? ? ? ? ? ? ? ? ? ? int _arg0;
? ? ? ? ? ? ? ? ? ? _arg0 = data.readInt();
? ? ? ? ? ? ? ? ? ? int _arg1;
? ? ? ? ? ? ? ? ? ? _arg1 = data.readInt();
? ? ? ? ? ? ? ? ? ? int _result = this.add(_arg0, _arg1); //调用我们实现好的方法
? ? ? ? ? ? ? ? ? ? reply.writeNoException();
? ? ? ? ? ? ? ? ? ? reply.writeInt(_result); //把结果返回
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case TRANSACTION_minus: {
? ? ? ? ? ? ? ? ? ? data.enforceInterface(DESCRIPTOR);
? ? ? ? ? ? ? ? ? ? int _arg0;
? ? ? ? ? ? ? ? ? ? _arg0 = data.readInt();
? ? ? ? ? ? ? ? ? ? int _arg1;
? ? ? ? ? ? ? ? ? ? _arg1 = data.readInt();
? ? ? ? ? ? ? ? ? ? int _result = this.minus(_arg0, _arg1);
? ? ? ? ? ? ? ? ? ? reply.writeNoException();
? ? ? ? ? ? ? ? ? ? reply.writeInt(_result);
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }