本文共 17675 字,大约阅读时间需要 58 分钟。
package com.xxq2dream.aidl;public interface IBookManager extends android.os.IInterface { /** * Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.xxq2dream.aidl.IBookManager { private static final java.lang.String DESCRIPTOR = "com.xxq2dream.aidl.IBookManager"; /** * Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * 用于将服务端的Binder对象转换成客户端所需要的AIDL接口类型的对象 */ public static com.xxq2dream.aidl.IBookManager asInterface(android.os.IBinder obj) { if ((obj == null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); //这里通过instanceof方法判断是否是跨进程,如果客户端和服务端位于同一进程,则返回的是服务端的Stub对象本身,不会调用跨进程的transact方法 if (((iin != null) && (iin instanceof com.xxq2dream.aidl.IBookManager))) { return ((com.xxq2dream.aidl.IBookManager) iin); } //如果客户端和服务端不在同一个进程中,那么返回的是系统封装好的Stub.Proxy代理对象,客户端调用会走transact方法发起跨进程请求 return new com.xxq2dream.aidl.IBookManager.Stub.Proxy(obj); } //返回当前Binder对象 @Override public android.os.IBinder asBinder() { return this; } /** * 运行在服务端的Binder线程池中 * * @param code 要运行的方法标识 * @param data 客户端传递过来的参数 * @param reply 回传给客户端的数据 * @param flags * @return true 表示执行成功;false,执行失败,客户端的请求失败 * @throws RemoteException */ @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_getBookList: { data.enforceInterface(DESCRIPTOR); java.util.List_result = this.getBookList(); reply.writeNoException(); //将结果写入reply返回 reply.writeTypedList(_result); return true; } case TRANSACTION_addBook: { data.enforceInterface(DESCRIPTOR); com.xxq2dream.aidl.Book _arg0; if ((0 != data.readInt())) { _arg0 = com.xxq2dream.aidl.Book.CREATOR.createFromParcel(data); } else { _arg0 = null; } this.addBook(_arg0); reply.writeNoException(); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.xxq2dream.aidl.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; } /** * 运行在客户端的Binder线程池中 * 当客户端和服务端不在同一个进程中时,通过上面的asInterface方法返回的就是这个Proxy对象 * 调用服务端的方法就是调用这里对应的方法,最终走的是transact方法发起远程过程调用 * * @return * @throws RemoteException */ @Override public java.util.List getBookList() throws android.os.RemoteException { //用于向服务端传递参数 android.os.Parcel _data = android.os.Parcel.obtain(); //用于接收服务端传回来的结果 android.os.Parcel _reply = android.os.Parcel.obtain(); java.util.List _result; try { _data.writeInterfaceToken(DESCRIPTOR); //发起远程过程调用请求,同时当前线程挂起,服务端的onTransact方法会被调用 mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0); //上述调用过程返回后,当前线程继续执行,从reply中取出结果返回 _reply.readException(); _result = _reply.createTypedArrayList(com.xxq2dream.aidl.Book.CREATOR); } finally { _reply.recycle(); _data.recycle(); } return _result; } @Override public void addBook(com.xxq2dream.aidl.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(); } } } //接口的标识 static final int TRANSACTION_getBookList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); static final int TRANSACTION_addBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1); } //提供的接口 public java.util.List getBookList() throws android.os.RemoteException; public void addBook(com.xxq2dream.aidl.Book book) throws android.os.RemoteException;}
public interface IBookManager extends IInterface { static final String DESCRIPTOR = "com.xxq2dream.manualbinder.IBookManager"; static final int TRANSACTION_getBookList = IBinder.FIRST_CALL_TRANSACTION + 0; static final int TRANSACTION_addBook = IBinder.FIRST_CALL_TRANSACTION + 1; public ListgetBookList() throws RemoteException; public void addBook(Book book) throws RemoteException; }
public class BookManagerImpl extends Binder implements IBookManager { private static final String TAG = "BookManagerImpl"; public BookManagerImpl() { Log.e(TAG, "attachInterface-->"+ System.currentTimeMillis()); this.attachInterface(this,DESCRIPTOR); } /** * Cast an IBinder object into an com.xxq2dream.IBookManager interface, * generating a proxy if needed. */ public static IBookManager asInterface(IBinder obj) { if ((obj == null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin != null) && (iin instanceof IBookManager))) { Log.e(TAG, "asInterface"); return ((IBookManager) iin); } Log.e(TAG, "asInterface Proxy-->"+ System.currentTimeMillis()); return new BookManagerImpl.Proxy(obj); } /** * 运行在服务端的Binder线程池中 * * @param code 要运行的方法标识 * @param data 客户端传递过来的参数 * @param reply 回传给客户端的数据 * @param flags * @return true 表示执行成功;false,执行失败,客户端的请求失败 * @throws RemoteException */ @Override public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { Log.e(TAG, "onTransact-->"+System.currentTimeMillis()); switch (code) { case INTERFACE_TRANSACTION: { Log.e(TAG, "INTERFACE_TRANSACTION-->"+ System.currentTimeMillis()); reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_getBookList: { Log.e(TAG, "TRANSACTION_getBookList-->"+ System.currentTimeMillis()); data.enforceInterface(DESCRIPTOR); Listresult = this.getBookList(); reply.writeNoException(); reply.writeTypedList(result); return true; } case TRANSACTION_addBook: { Log.e(TAG, "TRANSACTION_addBook-->"+ System.currentTimeMillis()); data.enforceInterface(DESCRIPTOR); Book arg0; if (0 != data.readInt()) { arg0 = Book.CREATOR.createFromParcel(data); }else { arg0 = null; } this.addBook(arg0); reply.writeNoException(); return true; } " +System.currentTimeMillis());--> " +System.currentTimeMillis());--> } return super.onTransact(code, data, reply, flags); } @Override public List getBookList() throws RemoteException { return null; } @Override public void addBook(Book book) throws RemoteException { } /** * Retrieve the Binder object associated with this interface. * You must use this instead of a plain cast, so that proxy objects * can return the correct result. */ @Override public IBinder asBinder() { Log.e(TAG, "asBinder-->"+ System.currentTimeMillis()); return this; } private static class Proxy implements IBookManager { private IBinder mRemote; Proxy(IBinder remote) { mRemote = remote; } public String getInterfaceDescriptor() { return DESCRIPTOR; } /** * 运行在客户端的Binder线程池中 * * @return * @throws RemoteException */ @Override public List getBookList() throws RemoteException { Log.e(TAG, "Proxy-->getBookList-->"+ System.currentTimeMillis()); //用于向服务端传递参数 Parcel data = Parcel.obtain(); //用于接收服务端传回来的结果 Parcel reply = Parcel.obtain(); List result; try { data.writeInterfaceToken(DESCRIPTOR); //发起远程过程调用请求,同时当前线程挂起,服务端的onTransact方法会被调用 mRemote.transact(TRANSACTION_getBookList, data, reply, 0); //上述调用过程返回后,当前线程继续执行,从reply中取出结果返回 reply.readException(); result = reply.createTypedArrayList(Book.CREATOR); } finally { reply.recycle(); data.recycle(); } return result; } @Override public void addBook(Book book) throws RemoteException { Log.e(TAG, "Proxy-->addBook-->"+ System.currentTimeMillis()); //用于向服务端传递参数 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(); } } registerListener-->"+ System.currentTimeMillis());--> unRegisterListener-->"+ System.currentTimeMillis());--> /** * Retrieve the Binder object associated with this interface. * You must use this instead of a plain cast, so that proxy objects * can return the correct result. */ @Override public IBinder asBinder() { Log.e(TAG, "Proxy-->asBinder-->"+ System.currentTimeMillis()); return mRemote; } }}
public interface IOnNewBookArrivedListener extends IInterface { static final String DESCRIPTOR = "com.xxq2dream.aidl.IOnNewBookArrivedListener"; static final int TRANSACTION_onNewBookArrived = IBinder.FIRST_CALL_TRANSACTION + 0; void onNewBookArrived(Book book) throws RemoteException;}
public class OnNewBookArrivedListenerImpl extends Binder implements IOnNewBookArrivedListener { private static final String TAG = "NewBookArrivedListener"; public OnNewBookArrivedListenerImpl() { Log.e(TAG, "attachInterface-->"+ System.currentTimeMillis()); this.attachInterface(this,DESCRIPTOR); } public static IOnNewBookArrivedListener asInterface (IBinder obj) { if ((obj == null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin != null) && (iin instanceof IOnNewBookArrivedListener))) { Log.e(TAG, "asInterface"); return ((IOnNewBookArrivedListener) iin); } Log.e(TAG, "asInterface Proxy-->"+ System.currentTimeMillis()); return new OnNewBookArrivedListenerImpl.Proxy(obj); } /** * 运行在服务端的Binder线程池中 * * @param code 要运行的方法标识 * @param data 客户端传递过来的参数 * @param reply 回传给客户端的数据 * @param flags * @return true 表示执行成功;false,执行失败,客户端的请求失败 * @throws RemoteException */ @Override public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { Log.e(TAG, "onTransact-->"+System.currentTimeMillis()); switch (code) { case INTERFACE_TRANSACTION: { Log.e(TAG, "INTERFACE_TRANSACTION-->"+ System.currentTimeMillis()); reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_onNewBookArrived: { Log.e(TAG, "TRANSACTION_onNewBookArrived-->"+ System.currentTimeMillis()); data.enforceInterface(DESCRIPTOR); Book _arg0; if ((0 != data.readInt())) { _arg0 = Book.CREATOR.createFromParcel(data); } else { _arg0 = null; } this.onNewBookArrived(_arg0); reply.writeNoException(); return true; } } return super.onTransact(code, data, reply, flags); } @Override public void onNewBookArrived(Book book) { } /** * Retrieve the Binder object associated with this interface. * You must use this instead of a plain cast, so that proxy objects * can return the correct result. */ @Override public IBinder asBinder() { return this; } private static class Proxy implements IOnNewBookArrivedListener { private IBinder mRemote; Proxy(IBinder remote) { mRemote = remote; } public String getInterfaceDescriptor() { return DESCRIPTOR; } /** * Retrieve the Binder object associated with this interface. * You must use this instead of a plain cast, so that proxy objects * can return the correct result. */ @Override public IBinder asBinder() { Log.e(TAG, "Proxy-->asBinder-->"+ System.currentTimeMillis()); return mRemote; } @Override public void onNewBookArrived(Book newBook) throws RemoteException{ Log.e(TAG, "onNewBookArrived-->" + System.currentTimeMillis()); android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); if ((newBook != null)) { _data.writeInt(1); newBook.writeToParcel(_data, 0); } else { _data.writeInt(0); } mRemote.transact(TRANSACTION_onNewBookArrived, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } }}
IOnNewBookArrivedListener arg0;arg0 = OnNewBookArrivedListenerImpl.asInterface(data.readStrongBinder());
欢迎关注我的微信公众号,和我一起学习一起成长!
转载地址:http://roxto.baihongyu.com/