Android学习笔记——AIDL与Binder(大坑待填)

Binder主要用在Service中,包括AIDL和Messenger。AIDL是用来辅助生成Binder的一种途径。
一、AIDL的使用
AIDL生成Binder还没有调通(就是绑定不上远程Service啊怎么回事),好像没脸写AIDL的使用了,就把别人是使用方式链接贴上来吧:
https://www.jianshu.com/p/29999c1a93cd

关于AIDL生成的Binder,姑且权当做一篇笔记,日后再逐渐完善吧:(Service是一个挺大的坑啊,绑定的时候都无法获得IBinder)

二、自己动手实现Binder
参考书籍《Android开发艺术探究》,不借助AIDL而手动 实现是为了熟悉Binder的跨进程通信机制。
2.1实现Parcelable
自己的类需要实现Parcelable接口才能用于Binder的跨进程通信。

public class Book implements Parcelable {
    public int bookId;
    public String bookName;

    protected Book(Parcel in) {
        bookId = in.readInt();
        bookName = in.readString();
    }

    //实现序列化功能
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(bookId);
        dest.writeString(bookName);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    //实现反序列化功能
    public static final Creator<Book> CREATOR = new Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel in) {
            return new Book(in);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };
}

2.2声明一个AIDL性质的接口,继承IInterface

public interface IBookManager extends IInterface {
    //Binder的唯一标识
    String DESCRIPTOR="com.example.licl.myapplication.binder.IBookManager";
    //方法的标识号
    int TRANSACTION_getBookList=IBinder.FIRST_CALL_TRANSACTION;
    int TRANSACTION_addBook=IBinder.FIRST_CALL_TRANSACTION+1;
    List<Book> getBookList() throws RemoteException;
    void addBook(Book book) throws RemoteException;
}

2.3实现Stub类以及其代理类

public class BookManagerImpl extends Binder implements IBookManager {
    public BookManagerImpl(){
        this.attachInterface(this,DESCRIPTOR);
    }
    //根据传进来的IBinder返回具体的BookManager类型,如果是同一进程的Binder则返回BookManagerImpl
    // 否则返回其代理

    public static IBookManager asInterface(IBinder obj){
        if(obj==null){
            return null;
        }
        IInterface iInterface=obj.queryLocalInterface(DESCRIPTOR);
        if(iInterface!=null&&iInterface instanceof IBookManager){
            return ((IBookManager)iInterface);
        }
        return new Proxy(obj);
    }

    //该方法运行在服务端的线程池中,当客户端发起跨进程请求时,远程请求会通过系统底层封装后调用此方法来处理
    @Override
    protected boolean onTransact(int code,  Parcel data, Parcel reply, int flags) throws RemoteException {
        switch (code){
            case INTERFACE_TRANSACTION:{
                reply.writeString(DESCRIPTOR);
                return true;
            }
            case TRANSACTION_getBookList:{
                data.enforceInterface(DESCRIPTOR);
                List<Book> result=this.getBookList();
                reply.writeNoException();
                reply.writeTypedList(result);
                return true;
            }
            case TRANSACTION_addBook:{
                data.enforceInterface(DESCRIPTOR);
                Book book;
                if(0!=data.readInt()){
                    book=Book.CREATOR.createFromParcel(data);
                }else{
                    book=null;
                }
                this.addBook(book);
                reply.writeNoException();
                return true;
            }
        }
        return super.onTransact(code, data, reply, flags);
    }

    @Override
    public List<Book> getBookList() throws RemoteException {
        return null;
    }

    @Override
    public void addBook(Book book) throws RemoteException {

    }

    @Override
    public IBinder asBinder() {
        return this;
    }

    //代理类
    private static class Proxy implements IBookManager{
        private IBinder mRemote;

        public Proxy(IBinder remote) {
            mRemote = remote;
        }

        public String getInterfaceDescriptor(){
            return DESCRIPTOR;
        }

        @Override
        public List<Book> getBookList() throws RemoteException {
            Parcel data=Parcel.obtain();
            Parcel reply=Parcel.obtain();
            List<Book> result;
            try{
                data.writeInterfaceToken(DESCRIPTOR);
                mRemote.transact(TRANSACTION_getBookList,data,reply,0);
                reply.readException();
                result=reply.createTypedArrayList(Book.CREATOR);
            }finally {
                reply.recycle();
                data.recycle();
            }
            return result;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            Parcel data=Parcel.obtain();
            Parcel reply=Parcel.obtain();
            try{
                data.writeInterfaceToken(DESCRIPTOR);
                if(book!=null){
                    data.writeInt(1);
                    book.writeToParcel(data,0);
                }else{
                    data.writeInt(0);
                }
                mRemote.transact(TRANSACTION_addBook,data,reply,0);
                reply.readException();
            }finally {
                reply.recycle();
                data.recycle();
            }
        }

        @Override
        public IBinder asBinder() {
            return mRemote;
        }
    }
}

完成上述几步后,自己手写Binder的步骤也基本完成了。

你可能感兴趣的