前言
AIDL (Android Interface Definition Language) 支持以下数据类型:
基本数据类型:int、long、float、double、boolean、char、byte。
字符串类型:String。
集合类型:List、Map、Set。
Parcelable 类型:实现了 Parcelable 接口的自定义类。
IBinder 类型:用于跨进程通信的 Binder 类型。
数组类型:int[]、String[]、Parcelable[] 等。
其他类型:CharSequence、SparseArray、Bundle、CharSequence[]、ArrayList 等。
所以,传输复杂类型的时候,只能使用Parcelable ,不支持Serializable
服务端
- 1、创建接收数据AIDL 文件
// IServerInterface.aidl
package com.rayvison.socketserviceapp;
import com.rayvison.socketserviceapp.ComplexData;
// Declare any non-default types here with import statements
interface IServerInterface {
void simpleData(String msg);
void complexData(in ComplexData complexData);
void destroyService();
void unregisterListener(com.rayvison.socketserviceapp.ICallBackInterface listener);
void registerListener(com.rayvison.socketserviceapp.ICallBackInterface listener);
}
- 2、创建接收接口AIDL 文件
// ICallBackInterface.aidl
package com.rayvison.socketserviceapp;
import com.rayvison.socketserviceapp.ComplexData;
// Declare any non-default types here with import statements
interface ICallBackInterface {
void onSimpleData(String msg);
void onComplexData(in ComplexData complexData);
}
- 3、创建实体类以及实体类AIDL 文件
实体类:
package com.rayvison.socketserviceapp;
import android.os.Parcel;
import android.os.Parcelable;
public class ComplexData implements Parcelable {
private float[][]handValue;
public ComplexData(){
}
protected ComplexData(Parcel in) {
handValue = new float[in.readInt()][in.readInt()];
for (int i = 0; i < handValue
AIDL跨进程通信服务端与客户端实现

博客介绍了AIDL支持的数据类型,如基本数据类型、字符串类型等,指出传输复杂类型时只能用Parcelable。还分别阐述了服务端创建接收数据、接口、实体类AIDL文件及binder的步骤,以及客户端拷贝文件、启动服务通信的流程,最后表示代码已上传GitHub。
最低0.47元/天 解锁文章

1269

被折叠的 条评论
为什么被折叠?



