车载Android应用开发-AIDL(一)

一、AIDL简介

AIDL(Android Interface Definition Language)是Android接口定义语言。作为车载Android开发中最常用的进程间通信手段,掌握其原理并熟练运用是十分有必要的。平时开发手机APP可能涉及到进程间通信的情况比较少,但车载应用会涉及到许多进程间的交互。

Android系统中通常一个进程无法直接访问另一个进程的内存空间。为了解决该问题,系统也提供了一些IPC手段如Messenger,它的底层也是基于AIDL实现的。

AIDL使用的是客户端--服务端架构,两者需要共享一个.aidl文件,用来声明通信接口,该文件会被Android SDK工具转换成一个Java接口,该接口包含一个Stub类和一个Proxy类。
客户端与服务端的主要作用如下:

客户端:指需要调用服务端提供的数据或功能的应用,它通过绑定服务端的Service来得到一个IBinder对象,通过该对象能够调用服务端暴露出来的接口。

服务端:指提供数据或功能给客户端的应用,它通过创建启动一个Service并在onBinder()方法中返回一个IBinder对象来暴露通信接口给客户端,使用该对象时需重写.aidl文件中定义的接口方法 。

二、AIDL使用

2.1 服务端

2.1.1 第一步创建aidl文件

首先在java目录同级创建一个aidl文件夹,在该文件夹下新建一个路径和工程包名同样的路径,之后在该路径下新建.aidl文件。

 AIDL文件是接口类型,我们只需声明好方法与参数即可。这里以一个两数相加的方法为例。

interface IMyAidlInterface {
    int add(int a, int b);
}

写好.aidl文件后可以通过AS编译工程,IDE就会生成对应的文件,路径如下:app/build/generated/aidl_source_output_dir/debug/compileDebugAidl/out/com/example/aidlserver/IMyAidlInterface

2.1.2 创建Service

服务端应用中创建一个Service,重写onBind方法并在其中返回一个IBinder对象,在new这个对象的时候会实现之前AIDL接口中声明的方法。

public class IRemoteService extends Service {

    private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
        @Override
        public int add(int a, int b) throws RemoteException {
            return a + b;
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}

创建好服务后,我们需要在AndroidMainFest中注册服务,并设置android:enabled和android:exported属性为true,以便其他应用可以访问它。

如果需要还可以添加一个intent-filter,指定一个action,让其他应用可以通过intent启动服务,同时服务端也可以通过读取intent中的action来过滤绑定请求。

<service 
    android:name=".IRemoteService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.example.aidlserver.IRemoteService" />
    </intent-filter>
</service>

最后在Activity中启动服务即可

startService(new Intent(this, IRemoteService.class));

在Android 8.0之后的系统中,Service启动后需要添加Notification,将Service设定为前台Service,否则会抛出异常。

 2.2 客户端

2.2.1 创建ServiceConnection

在java目录同级创建一个aidl文件夹,在该文件夹下新建一个路径和工程包名同样的路径,之后把服务端的AIDL文件完全复制过来。用IDE编译一下工程。

在客户端应用中,创建一个ServiceConnection对象,实现onServiceConnected和onServiceDisconnected方法,在onServiceConnected方法中获取IBinder对象的代理,并转换为AIDL接口类型。

private IMyAidlInterface iMyAidlInterface;
private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        iMyAidlInterface = null;
    }
};
2.2.2 绑定Service 

绑定服务端的Service,创建一个Intent,指定服务端的包名和Service的类名,如果服务端应用设置了intent-filter,还需要指定相应的action。

private void bindServer() {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.example.aidlserver", "com.example.aidlserver.IRemoteService"));
    intent.setAction("com.example.aidlserver.IRemoteService");
    boolean isConnect = bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    Log.e(TAG, "bindServer: " + isConnect);
}

获取到IBinder对象的代理后就可以通过该对象调用服务端提供的方法了。

注意:从Android 11 开始,系统对应用的可见性进行了保护,如果 build.gradle 中的Target API > = 30,那么还需要在 AndroidManifest.xml 配置queries标签指定服务端应用的包名,才可以绑定远程服务。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <queries>
        <package android:name="com.example.aidlserver"/>
    </queries>

</manifest>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A-sL1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值