Binder 学习笔记

跨进程通信(IPC)

1.AIDL MESSENGER(是封装好的AIDL)看了几遍没什么太深的印象,于是手写一个

照抄树上的栗子:

需要传输的数据 来一个:

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

    public Book(String bookName, int bookId) {
        this.bookName = bookName;
        this.bookId = bookId;
    }

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

    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];
        }
    };

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(bookName);
        dest.writeInt(bookId);
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", bookId=" + bookId +
                '}';
    }
}

2. 然后 IBookManager 来一个 里面主要定义了一个 DESCRIPTOR 来标识自己 然后两个方法 getBookList(),addBook(Book book)

public interface IBookManager extends IInterface {
    String DESCRIPTOR = "cn.uflycn.com.aidltest.aidl_test.aidl.IBookManager";

    List<Book> getBookList() throws RemoteException;

    void addBook(Book book) throws RemoteException;
}

3.然后再写它抽象的内部类Stub

 public static abstract class Stub extends Binder implements IBookManager {
        public static final int TRANSACTION_getBookList = Binder.FIRST_CALL_TRANSACTION + 0;
        public static final int TRANSACTION_addBook = Binder.FIRST_CALL_TRANSACTION + 1;

        public Stub() {
            attachInterface(this, DESCRIPTOR);
        }

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

        public static IBookManager asInterface(IBinder obj) {
            if (obj == null) {
                return null;
            }
            IInterface inn = obj.queryLocalInterface(DESCRIPTOR);
            if (inn != null && inn instanceof IBookManager) {
                return (IBookManager) inn;
            }
            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 = getBookList();
                    reply.writeNoException();
                    reply.writeTypedList(result);
                    return true;
                case TRANSACTION_addBook:
                    data.enforceInterface(DESCRIPTOR);
                    Book book;
                    if (data.readInt() != 0) {
                        book = Book.CREATOR.createFromParcel(data);
                    } else {
                        book = null;
                    }
                    this.addBook(book);
                    reply.writeNoException();
                    return true;
                default:
                    return super.onTransact(code, data, reply, flags);
            }
        }

        private static class Proxy implements IBookManager {
            private IBinder mRemote;

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

            @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;
            }
        }

    }

刚开始的时候忘记重写 ontransact 方法,获取到的结果都是空的,添加也加不进去,所以onTransct 是服务端处理数据返回结果的地方。

写了好几遍感觉

猜你喜欢

转载自blog.csdn.net/yyo201/article/details/80675029