1. 程式人生 > >AIDL深入理解

AIDL深入理解

一、 服務端

連線成功服務端會通過public IBinder onBind(Intent intent)方法 呼叫 return new IBookManager.Stub()將stub(也就是binder)類返回給客戶端 由於此類實現了IBookManager介面所以要實現已下五個方法(此方法就是在aidl介面檔案定義的方法) 並已下五個方法中做業務邏輯         1、int    add(int i,int j);         2、String getUserInfo(in Book book);         3、void   getOutList(out String[] list);         4、void   setInList(in String[] list);         5、void   getInOutList(inout String[] list);

 二、客戶端

連線成功後 客戶端會回撥 public void onServiceConnected(ComponentName componentName, IBinder iBinder)方法 並在此方法中呼叫iBase = IBookManager.Stub.asInterface(iBinder)方法 得到 IBookManager 類 也就是proxy類 Proxy類實現了IBookManager介面 也要實現了已下五個方法,此五種方法是給客戶端呼叫的            1、int    add(int i,int j);            2、String getUserInfo(in Book book);            3、void   getOutList(out String[] list);            4、void   setInList(in String[] list);            5、void   getInOutList(inout String[] list);當客戶端呼叫 iBase.add(10,20),實際呼叫的是 proxy中的add()方法如下: public int add(int i, int j) throws android.os.RemoteException {

     mRemote其實就是 stub(也就是binder)     mRemote呼叫transact(Stub.TRANSACTION_add, _data, _reply, 0)方法時     就不會走下面的方法了,此時在stub(也就是binder)類中的onTransact() 就會回撥,     在onTransact()中根據方法標示 取出_data和_reply 並呼叫 this.add(_arg0, _arg1);     也就是呼叫在服務端實現了的 add()方法 把返回結果放在_reply中 返回true     onTransact()返回true後 就繼續走下面的方法 最後把結果返回給客戶端

  android.os.Parcel _data = android.os.Parcel.obtain();//把方法需要的引數寫入這裡面(如果方法有引數)   android.os.Parcel _reply = android.os.Parcel.obtain();//把方法返回值寫入這裡面(如果方法有返回值)   int _result;   try {       _data.writeInterfaceToken(DESCRIPTOR);       _data.writeInt(i);       _data.writeInt(j);        mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);       _reply.readException();       _result = _reply.readInt();   } finally {       _reply.recycle();       _data.recycle();      }      return _result;

public class sdsds {
    public interface IBookManager extends android.os.IInterface {
        public static abstract class Stub extends android.os.Binder implements screen.com.mybinder.IBookManager {
            private static final java.lang.String DESCRIPTOR = "screen.com.mybinder.IBookManager";

            public Stub() {
                this.attachInterface(this, DESCRIPTOR);
            }
            public static screen.com.mybinder.IBookManager asInterface(android.os.IBinder obj) {
                if ((obj == null)) {
                    return null;
                }
                android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
                //如果在一個程序
                if (((iin != null) && (iin instanceof screen.com.mybinder.IBookManager))) {
                    return ((screen.com.mybinder.IBookManager) iin);
                }
                //客戶端服務端不再一個執行緒 大多數走這個方法
                return new screen.com.mybinder.IBookManager.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 {
                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_getUserInfo: {
                        data.enforceInterface(DESCRIPTOR);
                        screen.com.mybinder.Book _arg0;
                        if ((0 != data.readInt())) {
                            _arg0 = screen.com.mybinder.Book.CREATOR.createFromParcel(data);
                        } else {
                            _arg0 = null;
                        }
                        java.lang.String _result = this.getUserInfo(_arg0);
                        reply.writeNoException();
                        reply.writeString(_result);
                        return true;
                    }
                    case TRANSACTION_getOutList: {
                        data.enforceInterface(DESCRIPTOR);
                        java.lang.String[] _arg0;
                        int _arg0_length = data.readInt();
                        if ((_arg0_length < 0)) {
                            _arg0 = null;
                        } else {
                            _arg0 = new java.lang.String[_arg0_length];
                        }
                        this.getOutList(_arg0);
                        reply.writeNoException();
                        reply.writeStringArray(_arg0);
                        return true;
                    }
                    case TRANSACTION_setInList: {
                        data.enforceInterface(DESCRIPTOR);
                        java.lang.String[] _arg0;
                        _arg0 = data.createStringArray();
                        this.setInList(_arg0);
                        reply.writeNoException();
                        return true;
                    }
                    case TRANSACTION_getInOutList: {
                        data.enforceInterface(DESCRIPTOR);
                        java.lang.String[] _arg0;
                        _arg0 = data.createStringArray();
                        this.getInOutList(_arg0);
                        reply.writeNoException();
                        reply.writeStringArray(_arg0);
                        return true;
                    }
                }
                return super.onTransact(code, data, reply, flags);
            }

            private static class Proxy implements screen.com.mybinder.IBookManager {
                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 i, int j) 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(i);
                        _data.writeInt(j);
                        mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
                        _reply.readException();
                        _result = _reply.readInt();
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                    return _result;
                }

                @Override
                public java.lang.String getUserInfo(screen.com.mybinder.Book book) throws android.os.RemoteException {
                    android.os.Parcel _data = android.os.Parcel.obtain();
                    android.os.Parcel _reply = android.os.Parcel.obtain();
                    java.lang.String _result;
                    try {
                        _data.writeInterfaceToken(DESCRIPTOR);
                        if ((book != null)) {
                            _data.writeInt(1);
                            book.writeToParcel(_data, 0);
                        } else {
                            _data.writeInt(0);
                        }
                        mRemote.transact(Stub.TRANSACTION_getUserInfo, _data, _reply, 0);
                        _reply.readException();
                        _result = _reply.readString();
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                    return _result;
                }

                @Override
                public void getOutList(java.lang.String[] list) throws android.os.RemoteException {
                    android.os.Parcel _data = android.os.Parcel.obtain();
                    android.os.Parcel _reply = android.os.Parcel.obtain();
                    try {
                        _data.writeInterfaceToken(DESCRIPTOR);
                        if ((list == null)) {
                            _data.writeInt(-1);
                        } else {
                            _data.writeInt(list.length);
                        }
                        mRemote.transact(Stub.TRANSACTION_getOutList, _data, _reply, 0);
                        _reply.readException();
                        _reply.readStringArray(list);
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                }

                @Override
                public void setInList(java.lang.String[] list) throws android.os.RemoteException {
                    android.os.Parcel _data = android.os.Parcel.obtain();
                    android.os.Parcel _reply = android.os.Parcel.obtain();
                    try {
                        _data.writeInterfaceToken(DESCRIPTOR);
                        _data.writeStringArray(list);
                        mRemote.transact(Stub.TRANSACTION_setInList, _data, _reply, 0);
                        _reply.readException();
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                }

                @Override
                public void getInOutList(java.lang.String[] list) throws android.os.RemoteException {
                    android.os.Parcel _data = android.os.Parcel.obtain();
                    android.os.Parcel _reply = android.os.Parcel.obtain();
                    try {
                        _data.writeInterfaceToken(DESCRIPTOR);
                        _data.writeStringArray(list);
                        mRemote.transact(Stub.TRANSACTION_getInOutList, _data, _reply, 0);
                        _reply.readException();
                        _reply.readStringArray(list);
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                }
            }

            static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
            static final int TRANSACTION_getUserInfo = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
            static final int TRANSACTION_getOutList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
            static final int TRANSACTION_setInList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
            static final int TRANSACTION_getInOutList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 4);
        }

        public int add(int i, int j) throws android.os.RemoteException;

        public java.lang.String getUserInfo(screen.com.mybinder.Book book) throws android.os.RemoteException;

        public void getOutList(java.lang.String[] list) throws android.os.RemoteException;

        public void setInList(java.lang.String[] list) throws android.os.RemoteException;

        public void getInOutList(java.lang.String[] list) throws android.os.RemoteException;
    }

}