网站价值 批量查询百度健康
news/
2025/9/23 15:50:11/
文章来源:
网站价值 批量查询,百度健康,网络培训学院,网站充值平台怎么做参考#xff1a;Android进阶——AIDL详解_android aidl-CSDN博客
AIDL#xff08;Android 接口定义语言#xff09;#xff0c;可以使用它定义客户端与服务端进程间通信#xff08;IPC#xff09;的编程接口#xff0c;在 Android 中#xff0c;进程之间无法共享内存Android进阶——AIDL详解_android aidl-CSDN博客
AIDLAndroid 接口定义语言可以使用它定义客户端与服务端进程间通信IPC的编程接口在 Android 中进程之间无法共享内存用户空间不同进程之间的通信一般使用 AIDL 来处理。
使用流程:
在 .aidl 文件中定义 AIDL 接口并将其添加到应用工程的 src 目录下创建完成之后 rebuil Android SDK 工具会自动生成基于该 .aidl 文件的 IBinder 接口具体的业务对象实现这个接口这个具体的业务对象也是 IBinder 对象当绑定服务的时候会根据实际情况返回具体的通信对象本地还是代理 将客户端绑定到该服务上之后就可以调用 IBinder 中的方法来进行进程间通信IPC实际起作用的并不是AIDL文件而是据此而生成的一个IInterface的实例代码AIDL其实是为了避免我们重复编写代码而出现的一个模板
在实现AIDL的过程中服务端APP和客户端APP中要包含结构完全相同的AIDL接口文件包括AIDL接口所在的包名及包路径要完全一样否则就会报错这是因为客户端需要反序列化服务端中所有和AIDL相关的类如果类的完整路径不一致就无法反序列化成功。
小技巧为了更加方便的创建AIDL文件我们可以新建一个lib工程让客户端APP和服务端APP同时依赖这个lib这样只需要在这个lib工程中添加AIDL文件就可以了
如果不需要跨不同程序使用Binder来创建接口如果不需要处理多线程使用Messenger来处理使用AIDL务必理解绑定服务。 抽象类stub其继承了android.os.Binder、实现IaidlData接口故我们实际需要实现的是Stub抽象类。
package com.yqw.servicedemo; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import com.yqw.mylibrary.IMyAidlInterface; public class MyService extends Service { public static final String TAG MyService; public MyService() { } Override public IBinder onBind(Intent intent) { // Return the communication channel to the service. return binder; } private IMyAidlInterface.Stub binder new IMyAidlInterface.Stub() { Override public int borrowBook(String bookName) throws RemoteException { //TODO 实现一系列 借书 的逻辑后返回结果码比如:0为失败1为成功 return 1; } Override public int returnBook(String bookName) throws RemoteException { //TODO 实现一系列 还书 的逻辑后返回结果码比如:0为失败1为成功 return 1; } }; } ———————————————— 版权声明本文为博主原创文章遵循 CC 4.0 BY-SA 版权协议转载请附上原文出处链接和本声明。 原文链接https://blog.csdn.net/you943047219/article/details/109493490
package com.yqw.aidldemo; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.widget.Toast; import com.yqw.mylibrary.IMyAidlInterface; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private String TAG MainActivity; private IMyAidlInterface iMyAidlInterface; private boolean isConnected false;//是否连接服务 Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } Override protected void onStart() { super.onStart(); if (!isConnected) { mBindService(); } } Override protected void onStop() { super.onStop(); if (isConnected) { unbindService(mServiceConnection); isConnected false; } } private void mBindService() { Intent intent new Intent(); intent.setAction(com.yqw.servicedemo.MyService); intent.setPackage(com.yqw.servicedemo); bindService(intent, mServiceConnection, BIND_AUTO_CREATE); } private final ServiceConnection mServiceConnection new ServiceConnection() { Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.d(TAG, onServiceConnected); //获得 aidl定义的接口持有类 iMyAidlInterface IMyAidlInterface.Stub.asInterface(iBinder); isConnected true; } Override public void onServiceDisconnected(ComponentName componentName) { Log.d(TAG, onServiceDisconnected); isConnected false; } }; private void showToast(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } public void borrowBook(View view) { if (!isConnected) { showToast(未连接服务); return; } try { int resultCode iMyAidlInterface.borrowBook(流浪地球); showToast(resultCode 1 ? 借书成功 : 借书失败); } catch (RemoteException e) { e.printStackTrace(); } } public void returnBook(View view) { if (!isConnected) { showToast(未连接服务); return; } try { int resultCode iMyAidlInterface.returnBook(流浪地球); showToast(resultCode 1 ? 还书成功 : 还书失败); } catch (RemoteException e) { e.printStackTrace(); } } }
AIDL支持实体类但必须是实现了Parcelable接口支持序列化。 // Book.aidl package com.yqw.mylibrary; parcelable Book; package com.yqw.mylibrary; public class Book { String name; int num; public Book(String name, int num) { this.name name; this.num num; } public String getName() { return name; } public void setName(String name) { this.name name; } public int getNum() { return num; } public void setNum(int num) { this.num num; } } 然后实现 Parcelable接口按AltEnter自动补充好需要的代码
package com.yqw.mylibrary; import android.os.Parcel; import android.os.Parcelable; public class Book implements Parcelable { String name; int num; public Book(String name, int num) { this.name name; this.num num; } protected Book(Parcel in) { name in.readString(); num in.readInt(); } public static final CreatorBook CREATOR new CreatorBook() { Override public Book createFromParcel(Parcel in) { return new Book(in); } Override public Book[] newArray(int size) { return new Book[size]; } }; public String getName() { return name; } public void setName(String name) { this.name name; } public int getNum() { return num; } public void setNum(int num) { this.num num; } Override public int describeContents() { return 0; } Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(num); } } // IMyAidlInterface.aidl package com.yqw.mylibrary; import com.yqw.mylibrary.Book; interface IMyAidlInterface { int borrowBook(String bookName); int returnBook(String bookName); ListBook getBookList(); }
然后调用 public void getBooksNum(View view) { checkConnected(); try { int booksNum iMyAidlInterface.getBookList().size(); showToast(剩余图书数量 booksNum); } catch (RemoteException e) { e.printStackTrace(); } }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/913053.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!