aidl demo调用原理
通過網(wǎng)絡(luò)的例子,生成的BookManager如下所示:service和client都是相同的java文件生成的。
這個(gè)類的例子添加book和查詢book兩個(gè)例子
客戶端創(chuàng)建bookmananger方法:
bookManager = BookManager.Stub.asInterface(iBinder);客戶端里的iBinder,是binderProxy類的
創(chuàng)建一個(gè)proxy的例子
點(diǎn)擊添加后
點(diǎn)擊后調(diào)用
mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);----》》
調(diào)用BinderProxy類里的public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException?
?
?
接著調(diào)用Binder里面的execTransact,執(zhí)行了native函數(shù)后,會自動執(zhí)行下面的圖片函數(shù),進(jìn)入service服務(wù)端的代碼,下面代碼是單獨(dú)的進(jìn)程,在binder
進(jìn)程里面,所以不需要擔(dān)心這里的操作。
?
執(zhí)行BookManager里面的onTransact方法,?
服務(wù)端代碼如下圖所示:實(shí)現(xiàn)了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 statementspublic 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);}@Overridepublic android.os.IBinder asBinder() {return this;}@Overridepublic 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;}@Overridepublic android.os.IBinder asBinder() {return mRemote;}public java.lang.String getInterfaceDescriptor() {return DESCRIPTOR;}@Overridepublic 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;}@Overridepublic 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();}}}} }
總結(jié):1、參數(shù)的傳遞過程,首先通過序列化在main線程里,進(jìn)行轉(zhuǎn)換成序列化數(shù)據(jù),傳遞給remote對象,執(zhí)行后remote結(jié)果參數(shù)會自動轉(zhuǎn)換成方法的返回值;在binder進(jìn)程里面會將序列化數(shù)據(jù)轉(zhuǎn)換成類進(jìn)行處理,處理的結(jié)果在binder線程里面也要轉(zhuǎn)換成序列化數(shù)據(jù),跨進(jìn)程通訊只能傳遞序列化數(shù)據(jù),傳遞實(shí)體類
2、客戶端調(diào)用aild接口時(shí)候,會阻塞在主線程,所以耗時(shí)對 操作不要放在接口方法中,客戶端通過transactNative方法進(jìn)行阻塞主線程,通過回調(diào)的方式返回參數(shù)
轉(zhuǎn)載于:https://www.cnblogs.com/istar/p/10632428.html
總結(jié)
以上是生活随笔為你收集整理的aidl demo调用原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MPU6050参考代码
- 下一篇: 匈牙利算法小结