aidl demo调用原理

通过网络的例子,生成的BookManager如下所示:service和client都是相同的java文件生成的。

这个类的例子添加book和查询book两个例子

客户端创建bookmananger方法:

bookManager = BookManager.Stub.asInterface(iBinder);
客户端里的iBinder,是binderProxy类的
创建一个proxy的例子

点击添加后

点击后调用

mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);----》》

调用BinderProxy类里的public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException 

 

 

接着调用Binder里面的execTransact,执行了native函数后,会自动执行下面的图片函数,进入service服务端的代码,下面代码是单独的进程,在binder

进程里面,所以不需要担心这里的操作。

 

执行BookManager里面的onTransact方法,

 


服务端代码如下图所示:

实现了Stub类的方法,

 




/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: F:\\new_file\\demo\\aidl\\AidlService-master\\app\\src\\main\\aidl\\com\\example\\aidldemo\\BookManager.aidl
 */
package com.example.aidldemo;
// Declare any non-default types here with import statements

public interface BookManager extends android.os.IInterface {
    public java.util.List<com.example.aidldemo.Book> getBooks() throws android.os.RemoteException;

    public void addBook(com.example.aidldemo.Book book) throws android.os.RemoteException;

    /**
     * Local-side IPC implementation stub class.
     */
    public static abstract class Stub extends android.os.Binder implements com.example.aidldemo.BookManager {
        static final int TRANSACTION_getBooks = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_addBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
        private static final java.lang.String DESCRIPTOR = "com.example.aidldemo.BookManager";

        /**
         * Construct the stub at attach it to the interface.
         */
        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        /**
         * Cast an IBinder object into an com.example.aidldemo.BookManager interface,
         * generating a proxy if needed.
         */
        public static com.example.aidldemo.BookManager asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.example.aidldemo.BookManager))) {
                return ((com.example.aidldemo.BookManager) iin);
            }
            return new com.example.aidldemo.BookManager.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;
                }
                case TRANSACTION_getBooks: {
                    data.enforceInterface(descriptor);
                    java.util.List<com.example.aidldemo.Book> _result = this.getBooks();
                    reply.writeNoException();
                    reply.writeTypedList(_result);
                    return true;
                }
                case TRANSACTION_addBook: {
                    data.enforceInterface(descriptor);
                    com.example.aidldemo.Book _arg0;
                    if ((0 != data.readInt())) {
                        _arg0 = com.example.aidldemo.Book.CREATOR.createFromParcel(data);
                    } else {
                        _arg0 = null;
                    }
                    this.addBook(_arg0);
                    reply.writeNoException();
                    return true;
                }
                default: {
                    return super.onTransact(code, data, reply, flags);
                }
            }
        }

        private static class Proxy implements com.example.aidldemo.BookManager {
            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 java.util.List<com.example.aidldemo.Book> getBooks() throws android.os.RemoteException {
                android.os.Parcel _data = android.os.Parcel.obtain();
                android.os.Parcel _reply = android.os.Parcel.obtain();
                java.util.List<com.example.aidldemo.Book> _result;
                try {
                    _data.writeInterfaceToken(DESCRIPTOR);
                    mRemote.transact(Stub.TRANSACTION_getBooks, _data, _reply, 0);
                    _reply.readException();
                    _result = _reply.createTypedArrayList(com.example.aidldemo.Book.CREATOR);
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
                return _result;
            }

            @Override
            public void addBook(com.example.aidldemo.Book book) 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 ((book != null)) {
                        _data.writeInt(1);
                        book.writeToParcel(_data, 0);
                    } else {
                        _data.writeInt(0);
                    }
                    mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);
                    _reply.readException();
                } finally {
                    _reply.recycle();
                    _data.recycle();
                }
            }
        }
    }
}

  总结:1、参数的传递过程,首先通过序列化在main线程里,进行转换成序列化数据,传递给remote对象,执行后remote结果参数会自动转换成方法的返回值;在binder进程里面会将序列化数据转换成类进行处理,处理的结果在binder线程里面也要转换成序列化数据,跨进程通讯只能传递序列化数据,传递实体类

2、客户端调用aild接口时候,会阻塞在主线程,所以耗时对 操作不要放在接口方法中,客户端通过transactNative方法进行阻塞主线程,通过回调的方式返回参数

posted on 2019-03-31 18:23  tistar  阅读(579)  评论(0编辑  收藏  举报