publicclassBookManagetImplextendsBinderimplementsIBookManager{//The concrete implementation of Binder
publicBookManagetImpl(){ this.attachInterface(this, DESCRIPTOR);// Register this IInterface,so that queryLocalInterface would use DESCRIPTOR to get IInterface. }
publicstatic IBookManager asInterface(IBinder binder){// Client invoke this method to obtain IBookManager inteface if (binder == null) { returnnull; } IInterface iin = binder.queryLocalInterface(DESCRIPTOR);// Obtain IInterface by DESCRIPTOR. if ((iin != null) && (iin instanceof IBookManager)) { return (IBookManager) iin; } returnnew BookManagetImpl.Proxy(binder); }
@OverrideprotectedbooleanonTransact(int code, Parcel data, Parcel reply, int flags)throws RemoteException {//server would progress RPC switch (code) { case INTERFACE_TRANSACTION: reply.writeString(DESCRIPTOR); returntrue; case TRANSACTION_getBOOKLIST: data.enforceInterface(DESCRIPTOR); List<Book> books = this.getBookList(); reply.writeNoException(); reply.writeTypedList(books);//write reslut into reply returntrue; case TRANSACTION_addBook: data.enforceInterface(DESCRIPTOR); Book book = null; if (0 != data.readInt()) { book = Book.CREATOR.createFromParcel(data); } this.addBook(book); reply.writeNoException(); returntrue; } returnsuper.onTransact(code, data, reply, flags); }