当前位置:   article > 正文

AIDL使用以及回调接口的实现

aidl

一、AIDL是什么?
AIDL是一个缩写,全称是Android Interface Definition Language,也就是Android接口定义语言,是跨进程通讯的一种方式。

二、使用步骤
1.服务端创建

在main目录下创建aidl文件夹, 创建包com.xxx.xxx, 创建xx.aidl文件,定义需要的接口,因为需要服务端请求异步数据,所以需要定义个回调接口和数据类bean。

1.1 IDataProvide.aidl 创建

  1. package com.xxx.xxx.aidl;
  2. import com..xxx.xxx.aidl.InPackageInfo;
  3. import com..xxx.xxx.aidl.InTaskCallback;
  4. interface IDataProvider {
  5. void getPackages(String packageName);
  6. void registerCallback(InTaskCallback callback); //注册接口
  7. void unregisterCallBack();//移除接口
  8. }

 1.2 InPackageInfo 数据类

  1. package com.xxx.xxx.aidl;
  2. import android.os.Parcel;
  3. import android.os.Parcelable;
  4. public class InPackageInfo implements Parcelable {
  5. private String status = "";
  6. private String startDate = "";
  7. public InPackageInfo (String s, String date){
  8. super();
  9. status = s;
  10. startDate = date;
  11. }
  12. protected InPackageInfo(Parcel in) {
  13. status = in.readString();
  14. startDate = in.readString();
  15. }
  16. public String getStatus() {
  17. return status;
  18. }
  19. public void setStatus(String status) {
  20. this.status = status;
  21. }
  22. public String getStartDate() {
  23. return startDate;
  24. }
  25. public void setStartDate(String startDate) {
  26. this.startDate = startDate;
  27. }
  28. public static final Creator<InPackageInfo> CREATOR = new Creator<InPackageInfo>() {
  29. @Override
  30. public InPackageInfo createFromParcel(Parcel in) {
  31. return new InPackageInfo(in);
  32. }
  33. @Override
  34. public InPackageInfo[] newArray(int size) {
  35. return new InPackageInfo[size];
  36. }
  37. };
  38. @Override
  39. public int describeContents() {
  40. return 0;
  41. }
  42. @Override
  43. public void writeToParcel(Parcel parcel, int i) {
  44. parcel.writeString(status);
  45. parcel.writeString(startDate);
  46. }
  47. }

1.3  InPackageInfo.aidl 创建

  1. package com.indibox.tv.aidl;
  2. parcelable InPackageInfo;

1.4 InTaskCallback.aidl 回调创建

  1. package com.xxx.xxx.aidl;
  2. import com.indibox.tv.aidl.InPackageInfo;
  3. interface InTaskCallback {
  4. void getPackages(inout List<InPackageInfo> packageName);
  5. }

1.5 创建服务类 DataProviderService,

  1. package com.xxx.xxx.aidl;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. public class DataProviderService extends Service {
  6. private DataProvider mDataProvider = new DataProvider();
  7. public DataProviderService(){
  8. }
  9. @Override
  10. public IBinder onBind(Intent intent) {
  11. return mDataProvider;
  12. }
  1. public class DataProvider extends IDataProvider.Stub {
  2. private static final String TAG = "DataProvider";
  3. @Override
  4. public void getPackages(String packageName) throws RemoteException {
  5. DataPackagesUtils.getInstance().getPackages(packageName);
  6. }
  7. @Override
  8. public void registerCallback(InTaskCallback callback) throws RemoteException {
  9. DataPackagesUtils.getInstance().setTaskCallback(callback);
  10. }
  11. @Override
  12. public void unregisterCallBack() throws RemoteException {
  13. DataPackagesUtils.getInstance().setTaskCallback(null);
  14. }
  15. }}

注:记得要在AndroidManifest.xml 文件注册DataProviderService,设置action(启动服务的标识)

至此,服务端的准备工作已经完成了,接下来实现客户端。

2.客户端实现

2.1 为了确保不出错,建议直接把服务端的aidl文件夹拷贝

2.2 客户端调用

  1. package com.xxx.aidldemo;
  2. import android.content.ComponentName;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.os.IBinder;
  7. import android.os.RemoteException;
  8. import android.util.Log;
  9. import com.xxx.xxx.aidl.IDataProvider;
  10. import com.xxx.xxx.aidl.InPackageInfo;
  11. import com.xxx.xxx.aidl.InTaskCallback;
  12. import java.util.List;
  13. public class XMAIDLManager {
  14. private Context mContext;
  15. private IDataProvider mDataProvider;
  16. private OnConnectionListener mListener;
  17. public XMAIDLManager(Context context, OnConnectionListener listener) {
  18. mContext = context;
  19. mListener = listener;
  20. }
  21. private ServiceConnection connection = new ServiceConnection() {
  22. @Override
  23. public void onServiceConnected(ComponentName name, final IBinder service) {
  24. Log.d("AIDL", "onServiceConnected");
  25. mDataProvider = IDataProvider.Stub.asInterface(service);
  26. try {
  27. mDataProvider.registerCallback(new TaskCallback()); //设置回调
  28. mDataProvider.getPackages("inditvpackage"); //启动请求
  29. } catch (RemoteException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. @Override
  34. public void onServiceDisconnected(ComponentName name) {
  35. mDataProvider = null;
  36. }
  37. };
  38. class TaskCallback extends InTaskCallback.Stub {
  39. @Override
  40. public void getPackages(List<InPackageInfo> packageName) throws RemoteException {
  41. Log.d("TaskCallback", "getPackages");
  42. //服务端异步请求数据回调接口
  43. }
  44. }
  45. public void startService() {
  46. //启动服务
  47. Intent intent = new Intent();
  48. intent.setAction("你的action");
  49. intent.setPackage("包名");
  50. mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE);
  51. }
  52. public void stopService() {
  53. try {
  54. mDataProvider.unregisterCallBack();
  55. mContext.unbindService(connection);
  56. } catch (RemoteException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. public interface OnConnectionListener {
  61. void onConnection(List<InPackageInfo> packageInfos);
  62. }
  63. }

 到此AIDL的跨进程的服务端和客户端就实现了,可以进行数据类bean(可自定义)的传递以及服务端异步回调数据给客户端。

总结
使用过程中主要是数据类型的传递,如果是基本的数据类型可以直接传递,当传递特定对象时需要对这个对应对象进行Parcelable 序列化操作,对应记得传对象参数是,需要标识 in /out/ inout,否则编译会出错。整体AIDL使用还是比较方便的,希望此文章会对大家有帮助。
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/122831
推荐阅读
相关标签
  

闽ICP备14008679号