日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

aidl demo调用原理

發(fā)布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aidl demo调用原理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

通過網(wǎng)絡(luò)的例子,生成的BookManager如下所示:service和client都是相同的java文件生成的。

這個類的例子添加book和查詢book兩個例子

客戶端創(chuàng)建bookmananger方法:

bookManager = BookManager.Stub.asInterface(iBinder);
客戶端里的iBinder,是binderProxy類的
創(chuàng)建一個proxy的例子

點擊添加后

點擊后調(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ù),進入service服務(wù)端的代碼,下面代碼是單獨的進程,在binder

進程里面,所以不需要擔心這里的操作。

?

執(zhí)行BookManager里面的onTransact方法,

?

服務(wù)端代碼如下圖所示:

實現(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線程里,進行轉(zhuǎn)換成序列化數(shù)據(jù),傳遞給remote對象,執(zhí)行后remote結(jié)果參數(shù)會自動轉(zhuǎn)換成方法的返回值;在binder進程里面會將序列化數(shù)據(jù)轉(zhuǎn)換成類進行處理,處理的結(jié)果在binder線程里面也要轉(zhuǎn)換成序列化數(shù)據(jù),跨進程通訊只能傳遞序列化數(shù)據(jù),傳遞實體類

2、客戶端調(diào)用aild接口時候,會阻塞在主線程,所以耗時對 操作不要放在接口方法中,客戶端通過transactNative方法進行阻塞主線程,通過回調(diào)的方式返回參數(shù)

轉(zhuǎn)載于:https://www.cnblogs.com/istar/p/10632428.html

總結(jié)

以上是生活随笔為你收集整理的aidl demo调用原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。