Aidl进程间通讯详细介绍

目录介绍

  • 1.问题答疑
  • 2.Aidl相关属性介绍
    • 2.1 AIDL所支持的数据类型
    • 2.2 服务端和客户端
    • 2.3 AIDL的基本概念
  • 3.实际开发中案例操做
    • 3.1 aidl通讯业务需求
    • 3.2 操做步骤伪代码
    • 3.3 服务端操做步骤
    • 3.4 客户端操做步骤
    • 3.5 测试
  • 4.可能出现的问题
    • 4.1 客户端在子线程中发起通讯访问问题
    • 4.2 什么状况下会致使远程调用失败
    • 4.3 设置aidl的权限,须要经过权限才能调用
  • 5.部分源码解析
    • 5.1 服务端aidl编译生成的java文件
    • 5.2 客户端绑定服务端service原理

关于aidl应用案例

1.问题答疑

  • 1.1.0 AIDL所支持的数据类型有哪些?
  • 1.1.1 提供给客户端链接的service何时运行?
  • 1.1.2 Stub类是干什么用的呢?
  • 1.1.3 如何解决远程调用失败的问题?

2.Aidl相关属性介绍

2.1 AIDL所支持的数据类型

  • 在AIDL中,并不是支持全部数据类型,他支持的数据类型以下所示:
    • 基本数据类型(int、long、char、boolean、double、float、byte、short)
    • String和CharSequence
    • List:只支持ArrayList,而且里面的每一个元素必须被AIDL支持
    • Map: 只支持HashMap, 一样的,里面的元素都必须被AIDL支持,包括key和value
    • Parcelable:全部实现了Parcelable接口的对象
    • AIDL: 全部的AIDL接口自己也能够在AIDL 文件中使用

2.2 服务端和客户端

  • 2.2.1 服务端php

    • 注意:服务端就是你要链接的进程。服务端给客户端一个Service,在这个Service中监听客户端的链接请求,而后建立一个AIDL接口文件,里面是将要实现的方法,注意这个方法是暴露给客户端的的。在Service中实现这个AIDL接口便可
  • 2.2.2 客户端java

    • 客户端首先须要绑定服务端的Service,绑定成功后,将服务端返回的Binder对象转换成AIDL接口所属的类型,最后调用AIDL的方法就能够了。

2.3 AIDL的基本概念

  • AIDL:Android Interface Definition Language,即Android接口定义语言;用于让某个Service与多个应用程序组件之间进行跨进程通讯,从而能够实现多个应用程序共享同一个Service的功能。

3.实际开发中案例操做

3.1 aidl通讯业务需求

  • aidl多进程通讯应用——服务端:某app;客户端:app调试工具。注意:aidl多进程通讯是指两个独立app之间的通讯……
  • 打开app调试工具,能够经过绑定服务端某app的service,获取到公司app的信息,好比渠道,版本号,签名,打包时间,token等属性
  • 经过app调试工具,能够经过aidl接口中的方法设置属性,设置成功后,查看某app是否设置属性成功

3.2 操做步骤伪代码

  • 3.2.1 服务端android

    • 步骤1:新建定义AIDL文件,并声明该服务须要向客户端提供的接口
    • 补充,若是aidl中有对象,则须要建立对象,而且实现Parcelable
    • 步骤2:在Service子类中实现AIDL中定义的接口方法,并定义生命周期的方法(onCreat、onBind()、blabla)
    • 步骤3:在AndroidMainfest.xml中注册服务 & 声明为远程服务
  • 3.2.2 客户端git

    • 步骤1:拷贝服务端的AIDL文件到目录下
    • 步骤2:使用Stub.asInterface接口获取服务器的Binder,根据须要调用服务提供的接口方法
    • 步骤3:经过Intent指定服务端的服务名称和所在包,绑定远程Service

3.3 服务端操做步骤

  • 3.3.1 建立一个aidl文件【注意:在main路径下建立】
    • 能够看到里面有一个AppInfo,注意这个类须要本身建立,而且手动导包进来。不然编译时找不到……
// ICheckAppInfoManager.aidl
package cn.ycbjie.ycaudioplayer;
import cn.ycbjie.ycaudioplayer.AppInfo;
// Declare any non-default types here with import statements

interface ICheckAppInfoManager {

    //获取app信息,好比token,版本号,签名,渠道等信息
    List<AppInfo> getAppInfo(String sign);
    
    boolean setToken(String sign,String token);

    boolean setChannel(String sign,String channel);

    boolean setAppAuthorName(String sign,String name);

}
复制代码
  • 3.3.2 建立一个AppInfo类,实现Parcelable接口
    • 这个类就是须要用的实体类,由于是跨进程,因此实现了Parcelable接口,这个是Android官方提供的,它里面主要是靠Parcel来传递数据,Parcel内部包装了可序列化的数据,可以在Binder中自由传输数据。
    • 注意:若是用到了自定义Parcelable对象,就须要建立一个同名的AIDL文件,包名要和实体类包名一致。我以前这个地方没加,致使出现错误!
    • 如图所示:
  • image
import android.os.Parcel;
import android.os.Parcelable;

public class AppInfo  implements Parcelable {

    private String key;
    private String value;

    public AppInfo(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.key);
        dest.writeString(this.value);
    }

    public AppInfo() {
    }

    protected AppInfo(Parcel in) {
        this.key = in.readString();
        this.value = in.readString();
    }

    public static final Creator<AppInfo> CREATOR = new Creator<AppInfo>() {
        @Override
        public AppInfo createFromParcel(Parcel source) {
            return new AppInfo(source);
        }

        @Override
        public AppInfo[] newArray(int size) {
            return new AppInfo[size];
        }
    };

    @Override
    public String toString() {
        return "AppInfo{" +
                "key='" + key + '\'' + ", value='" + value + '\'' + '}'; } } 复制代码
  • 3.3.3 在Service子类中实现AIDL中定义的接口方法,并定义生命周期的方法(onCreat、onBind()等)
    • 重写的onBinde()方法中返回Binder对象,这个Binder对象指向IAdvertManager.Stub(),这个Stub类并不是咱们本身建立的,而是AIDL自动生成的。系统会为每一个AIDL接口在build/source/aidl下生成一个文件夹,它的名称跟你命名的AIDL文件夹同样,里面的类也同样。
    • 建立binder对象,在这个getAppInfo方法中,能够设置app基本信息,方便后期多进程通讯测试
/**
 * <pre>
 *     @author yangchong
 *     blog  :
 *     time  : 2018/05/30
 *     desc  : 用于aidl多进程通讯服务service
 *     revise:
 * </pre>
 */
public class AppInfoService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogUtils.i("AppInfoService--IBinder:");
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.i("AppInfoService--onCreate:");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.i("AppInfoService--onDestroy:");
    }

    /**
     * 1.核心,Stub里面的方法运行的binder池中。
     * 2.Stub类并不是咱们本身建立的,而是AIDL自动生成的。
     *   系统会为每一个AIDL接口在build/generated/source/aidl下生成一个文件夹,它的名称跟你命名的AIDL文件夹同样
     * 3.Stub类,是一个内部类,他本质上是一个Binder类。当服务端和客户端位于同一个进程时,方法调用不会走跨进程的transact过程,
     *   当二者处于不一样晋城市,方法调用走transact过程,这个逻辑由Stub的内部代理类Proxy完成。
     */
    private final IBinder binder = new ICheckAppInfoManager.Stub() {
        @Override
        public List<AppInfo> getAppInfo(String sign) throws RemoteException {
            List<AppInfo> list=new ArrayList<>();
            String aidlCheckAppInfoSign = AppToolUtils.getAidlCheckAppInfoSign();
            LogUtils.e("AppInfoService--AppInfoService",aidlCheckAppInfoSign+"-------------"+sign);
            if(!aidlCheckAppInfoSign.equals(sign)){
                return list;
            }
            list.add(new AppInfo("app版本号(versionName)", BuildConfig.VERSION_NAME));
            list.add(new AppInfo("app版本名称(versionCode)", BuildConfig.VERSION_CODE+""));
            list.add(new AppInfo("打包时间", BuildConfig.BUILD_TIME));
            list.add(new AppInfo("app包名", getPackageName()));
            list.add(new AppInfo("app做者", SPUtils.getInstance(Constant.SP_NAME).getString("name","杨充")));
            list.add(new AppInfo("app渠道", SPUtils.getInstance(Constant.SP_NAME).getString("channel")));
            list.add(new AppInfo("token", SPUtils.getInstance(Constant.SP_NAME).getString("token")));
            list.add(new AppInfo("App签名", AppToolUtils.getSingInfo(getApplicationContext(), getPackageName(), AppToolUtils.SHA1)));
            return list;
        }


        @Override
        public boolean setToken(String sign, String token) throws RemoteException {
            if(!AppToolUtils.getAidlCheckAppInfoSign().equals(sign)){
                return false;
            }
            SPUtils.getInstance(Constant.SP_NAME).put("token",token);
            LogUtils.i("AppInfoService--setToken:"+ token);
            return true;
        }

        @Override
        public boolean setChannel(String sign, String channel) throws RemoteException {
            if(!AppToolUtils.getAidlCheckAppInfoSign().equals(sign)){
                return false;
            }
            SPUtils.getInstance(Constant.SP_NAME).put("channel",channel);
            LogUtils.i("AppInfoService--setChannel:"+ channel);
            return true;
        }

        @Override
        public boolean setAppAuthorName(String sign, String name) throws RemoteException {
            if(!AppToolUtils.getAidlCheckAppInfoSign().equals(sign)){
                return false;
            }
            SPUtils.getInstance(Constant.SP_NAME).put("name",name);
            LogUtils.i("AppInfoService--setAppAuthorName:"+ name);
            return true;
        }
    };
}
复制代码
  • 3.3.4 在AndroidMainfest.xml中注册服务 & 声明为远程服务
    • 在清单文件注册便可,须要设置action。这个在客户端中绑定服务service须要用到!
<service android:name=".service.AppInfoService"
    android:process=":remote"
    android:exported="true">
    <intent-filter>
        <action android:name="cn.ycbjie.ycaudioplayer.service.aidl.AppInfoService"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</service>
复制代码

3.4 客户端操做步骤

  • 3.4.1 拷贝服务端的AIDL文件到目录下github

    • 注意:复制时不要改动任何东西!
    • 如图所示:
      image
  • 3.4.2 经过Intent指定服务端的服务名称和所在包,绑定远程Servicesegmentfault

    • 经过Intent指定服务端的服务名称和所在包,进行Service绑定;
    • 建立ServiceConnection对象
    /**
     * 跨进程绑定服务
     */
    private void attemptToBindService() {
        Intent intent = new Intent();
        //经过Intent指定服务端的服务名称和所在包,与远程Service进行绑定
        //参数与服务器端的action要一致,即"服务器包名.aidl接口文件名"
        intent.setAction("cn.ycbjie.ycaudioplayer.service.aidl.AppInfoService");
        //Android5.0后没法只经过隐式Intent绑定远程Service
        //须要经过setPackage()方法指定包名
        intent.setPackage(packName);
        //绑定服务,传入intent和ServiceConnection对象
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }
    
    
    /**
     * 建立ServiceConnection的匿名类
     */
    private ServiceConnection connection = new ServiceConnection() {
        //重写onServiceConnected()方法和onServiceDisconnected()方法
        // 在Activity与Service创建关联和解除关联的时候调用
        @Override public void onServiceDisconnected(ComponentName name) {
            Log.e(getLocalClassName(), "没法绑定aidlServer的AIDLService服务");
            mBound = false;
        }
    
        //在Activity与Service创建关联时调用
        @Override public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(getLocalClassName(), "完成绑定aidlServer的AIDLService服务");
            //使用IAppInfoManager.Stub.asInterface()方法获取服务器端返回的IBinder对象
            //将IBinder对象传换成了mAIDL_Service接口对象
            messageCenter = ICheckAppInfoManager.Stub.asInterface(service);
            mBound = true;
            if (messageCenter != null) {
                try {
                    //连接成功
                    Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    复制代码
  • 3.4.3 使用Stub.asInterface接口获取服务器的Binder,根据须要调用服务提供的接口方法bash

    • 经过步骤3.4.2完成了跨进程绑定服务,接下来经过调用方法获取到数据。这里能够调用getAppInfo方法获取到服务端[app]的数据
    private void getAppInfo() {
        //若是与服务端的链接处于未链接状态,则尝试链接
        if (!mBound) {
            attemptToBindService();
            Toast.makeText(this, "当前与服务端处于未链接状态,正在尝试重连,请稍后再试",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        if (messageCenter == null) {
            return;
        }
        try {
            List<AppInfo> info = messageCenter.getAppInfo(Utils.getSign(packName));
            if(info==null || (info.size()==0)){
                Toast.makeText(this, "没法获取数据,多是签名错误!", Toast.LENGTH_SHORT).show();
            }else {
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                FirstAdapter adapter = new FirstAdapter(info, this);
                mRecyclerView.setAdapter(adapter);
                adapter.setOnItemClickListener(new FirstAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
    
                    }
                });
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    复制代码

3.5 测试

  • 最后看看经过测试工具[客户端]跨进程获取服务端app信息截图
    • 具体能够经过实际案例操做:后来发现跨进程通讯原来挺好玩的……项目地址:github.com/yangchong21…
    • 如图所示:
      image

4.可能出现的问题

4.1 客户端在子线程中发起通讯访问问题

  • 当客户端发起远程请求时,客户端会挂起,一直等到服务端处理完并返回数据,因此远程通讯是很耗时的,因此不能在子线程发起访问。因为服务端的Binder方法运行在Binder线程池中,因此应采起同步的方式去实现,由于它已经运行在一个线程中呢。

4.2 什么状况下会致使远程调用失败

  • Binder是会意外死亡的。若是服务端的进程因为某种缘由异常终止,会致使远程调用失败,若是咱们不知道Binder链接已经断裂, 那么客户端就会受到影响。不用担忧,Android贴心的为咱们提供了连个配对的方法linkToDeath和unlinkToDeath,经过linkToDeath咱们能够给Binder设置一个死亡代理,当Binder死亡时,咱们就会收到通知。
// 在建立ServiceConnection的匿名类中的onServiceConnected方法中
// 设置死亡代理
messageCenter.asBinder().linkToDeath(deathRecipient, 0);


/**
 * 给binder设置死亡代理
 */
private IBinder.DeathRecipient deathRecipient = new IBinder.DeathRecipient() {

    @Override
    public void binderDied() {
        if(messageCenter == null){
            return;
        }
        messageCenter.asBinder().unlinkToDeath(deathRecipient, 0);
        messageCenter = null;
        //这里从新绑定服务
        attemptToBindService();
    }
};
复制代码

4.3 设置aidl的权限,须要经过权限才能调用

<!--给aidl多进程通讯,服务加入权限验证功能-->
<permission android:name="aidl.AppInfoService"
    android:protectionLevel="normal"/>
    

//在AppInfoService服务中验证权限
@Nullable
@Override
public IBinder onBind(Intent intent) {
    LogUtils.i("AppInfoService--IBinder:");
    int check = checkCallingOrSelfPermission("aidl.AppInfoService");
    if(check == PackageManager.PERMISSION_DENIED){
        return null;
    }
    return binder;
}
复制代码

5.部分源码解析

5.1 服务端aidl编译生成的java文件

  • 5.1.1 首先找到aidl编译生成的Java文件 服务器

    image

  • 5.1.2 分析生成的java文件markdown

    • 这个ICheckAppInfoManager.java就是系统为咱们生成的相应java文件,简单说下这个类。它声明了三个方法getAppInfo,setToken和setChannel,分明就是咱们AIDL接口中的三个方法。同时他声明了3个id用来标识这几个方法,id用于标识在transact过程当中客户端请求的究竟是哪一个方法。接着就是咱们的Stub,能够看到它是一个内部类,他本质上是一个Binder类。当服务端和客户端位于同一个进程时,方法调用不会走跨进程的transact过程,当二者处于不一样晋城市,方法调用走transact过程,这个逻辑由Stub的内部代理类Proxy完成。
    • 这个Stub对象之因此里面有咱们AIDL的接口,正是由于官方替咱们作好了,咱们只要在这里具体实现就行了。

5.2 客户端绑定服务端service原理

  • 客户端也很是简单,首先咱们链接到服务端Service,在链接成功时,也就是onServiceConnected方法里,经过asInterface(service)方法能够将服务端的Binder对象转换成客户端所需的AIDL的接口的对象。这种转换是区分进程的,若是是同一进程,那么此方法返回的就是Stub自己,不然返回的就是系统Stub.proxy对象。拿到接口对象以后,咱们就可以调用相应方法进行本身的处理

参考文章

关于其余内容介绍

01.关于博客汇总连接

02.关于个人博客

相关文章
相关标签/搜索