使用AIDL进行Binder通信

1、客户端代码:

//在客户端中,可以通过bindService的回调中获取AIDL接口
private ServiceConnection conn = new ServiceConnection() {  
    @Override  
    public void onServiceConnected(ComponentName name, IBinder service) {  
        iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);  
    }  

    @Override  
    public void onServiceDisconnected(ComponentName name) {  
        iMyAidlInterface = null;  
    }  
};

//bindService
 Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.gyw.plugin_apk"
                , "com.gyw.plugin_apk.MyServer"));
        getActivity().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
//方法调用
public void onClick(View view) {  
    try {  
        int res = iMyAidlInterface.add(1, 2);  
        Log.i("gyw", "从服务端调用成功的结果:" + res);  
    } catch (RemoteException e) {  
        e.printStackTrace();
    }
} 

客户端调用流程:

调用Stub.asInterface获取BinderProxy对象

调用BinderProxy对象的add方法

2、服务端代码:

private IBinder myS = new IMyAidlInterface.Stub() {  
    @Override  
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {  

    }  

    @Override  
    public int add(int num1, int num2) throws RemoteException {  
        Log.i("Hensen", "从客户端发来的AIDL请求:num1->" + num1 + "::num2->" + num2);  
        return num1 + num2;  
    }  
}; 

3、生成的aidl文件解析:

Stub类继承自Binder,意味着这个Stub其实自己是一个Binder本地对象,然后实现了IMyAidlInterface接口,IMyAidlInterface本身是一个IInterface,因此他携带某种客户端需要的能力(这里是方法add)。此类有一个内部类Proxy,也就是Binder代理对象

public interface IMyAidlInterface extends android.os.IInterface
{
    //客户端通过这个Stub去获取binder的代理对象
    public static abstract class Stub extends android.os.Binder implements com.gyw.plugin_apk.IMyAidlInterface
    {
        //唯一性标识
         private static final java.lang.String DESCRIPTOR = "com.gyw.plugin_apk.IMyAidlInterface";
        /** Construct the stub at attach it to the interface. */
        public Stub()
        {
            this.attachInterface(this, DESCRIPTOR);
        }
        /**
         * Cast an IBinder object into an com.gyw.plugin_apk.IMyAidlInterface interface,
         * generating a proxy if needed.
         *根据 Binder 本地对象或者代理对象返回 IMyAidlInterface 接口
         *
         *服务端自己通信使用的就是本地的binder对象,跨进程使用的Binder的代理对象
         asInterface方法参数IBInder就是服务端的binder对象,如果是Binder本地对象,那么它就是Binder类型,如果是Binder代理对象,那就是BinderProxy类型。
         */
        public static com.gyw.plugin_apk.IMyAidlInterface asInterface(android.os.IBinder obj)
        {
            if ((obj==null)) {
                return null;
            }
            //查找本地对象
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin!=null)&&(iin instanceof com.gyw.plugin_apk.IMyAidlInterface))) {
                return ((com.gyw.plugin_apk.IMyAidlInterface)iin);
            }
             //获取代理对象
            return new com.gyw.plugin_apk.IMyAidlInterface.Stub.Proxy(obj);
        }
        @Override public android.os.IBinder asBinder()
        {
            return this;
        }
       //在Server进程里面,onTransact根据调用code会调用相关函数,接着将结果写入reply并通过super.onTransact返回给驱动,驱动唤醒挂起的Client进程里面的线程并将结果返回
        @Override 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();
                    //调用 Binder 本地对象
                    int _result = this.add(_arg0, _arg1);
                    reply.writeNoException();
                    reply.writeInt(_result);
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }
        private static class Proxy implements com.gyw.plugin_apk.IMyAidlInterface
        {
            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;
            }
            //当客户端调用add方法时,首先用Parcel把数据序列化,然后调用mRemote.transact方法,mRemote就是new Stub.Proxy(obj)传进来的,即BinderProxy对象
            @Override public int add(int x, int y) 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(x);
                    _data.writeInt(y);
                     //BinderProxy的transact方法是native方法,它的实现在native层,它会去借助Binder驱动完成数据的传输,当完成数据传输后,会唤醒Server端,调用了Server端本地对象的onTransact函数
                    mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.readInt();
                }
                finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }
        }
        //方法标识,用十六进制表示
        static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
    }
    //Server 具有的能力
    public int add(int x, int y) throws android.os.RemoteException;
}

你可能感兴趣的:(使用AIDL进行Binder通信)