一、AIDL的介绍
1.1 AIDL的定义:
aidl是为了实现进程间通信(特别是多进程并发情况下的通信)而设计的androi接口语言,全称Android Interface Definition Language,也就是Android接口定义语言
1.2 AIDL支持的数据类型:
(1)Java中的八种基本数据类型,包括byte、short、int、long、float、double、boolean、 char;
(2)String类型;
(3)CharSequence类型;
(4)List类型
(5)Map类型
(6)定向tag
二、AIDL实操
原理图:
AIDL实现分客户端和服务端(以aidl实现加法运算为例)
2.1服务端
首先创建一个AIDLServer工程,包的路径为com.example.aidlserver,我们添加一个aidl文件
// IMyAidlService.aidl
package com.example.aidlserver;
// Declare any non-default types here with import statements
interface IMyAidlService {
int add(int value1,int value2);
}
as同步一下生成aidl相关的java代码
然后添加一个service作为aidl的实体调用类
public class MyAidlService extends Service {
private final String TAG = "MyAidlService";
@Override
public void onCreate() {
super.onCreate();
}
private IMyAidlService.Stub iBinder = new IMyAidlService.Stub() {
@Override
public int add(int value1, int value2) throws RemoteException {
return value1+value2;
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, " MyAidlService onBind: ");
return iBinder;
}
}
同时注册一下该service
<service
android:name=".MyAidlService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.aidlserver"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
2.2客户端
接着我们创建一个aidl client工程,包名路径为com.example.aidlclient,在aidl文件夹下创建和服务端一样的aidl文件
// IMyAidlService.aidl
package com.example.aidlserver;
// Declare any non-default types here with import statements
interface IMyAidlService {
int add(int value1,int value2);
}
同样as对工程同步,生成aidl代码
然后我们再MainActivity进行调用代码如下,运行之后就可以进行aidl加法运算了
package com.example.aidlclient;
import android.content.ComponentName;
import android.content.Context;
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 androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.aidlserver.IMyAidlService;
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity";
private IMyAidlService iMyAidlService;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService();
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int sum = iMyAidlService.add(12, 12);
Log.i(TAG, "onServiceConnected sum:"+sum);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlService = IMyAidlService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
iMyAidlService = null;
}
};
private void bindService() {
Intent intent = new Intent();
intent.setAction("com.example.aidlserver");
intent.setPackage("com.example.aidlserver");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
针对android11以上设备清单文件里记得配置,防止报错
<queries>
<package android:name="com.example.aidlserver" />
<intent>
<action android:name="com.example.aidlserver" />
</intent>
</queries>