还在用AIDL吗?试试EasyMessenger吧

2022-10-28,,

EasyMessenger

直达Github项目地址

一款用于Android平台的基于Binder的进程间通信库,采用annotationProcessor生成IPC通信需要的代码。EasyMessenger相对于AIDL具备如下优势:

采用Java声明接口,更方便
接口方法支持重载
同时支持同步和异步通信

EasyMessenger目前支持如下数据类型:

boolean, byte, char, short, int, long, float, double
boolean[], byte[], char[], int[], long[], float[], double[]
String, String[]
Parcelable, Parcelable[]
Serializable
ArrayList
enum(需要实现parcelable)

下载

implementation 'cn.zmy:easymessenger-lib:0.1'
annotationProcessor 'cn.zmy:easymessenger-compilier:0.1'

开始使用

Client

声明接口:

@BinderClient
public interface ClientInterface
{
int add(int num1, int num2);
}

build之后,会生成ClientInterfaceHelper类,开发者也正是通过这个Helper类进行IPC通信。

//使用之前需要初始化
ClientInterfaceHelper.instance.__init(context,
new ComponentName("{server_package}", "{server_service_name}")); //同步IPC调用
int result = ClientInterfaceHelper.instance.add(1, 2); //异步IPC调用
ClientInterfaceHelper.instance.addAsync(1, 2, new IntCallback()
{
@Override
public void onSuccess(int result)
{
//调用成功
} @Override
public void onError(Exception ex)
{
//调用失败
}
});

Server

实现接口:

@BinderServer
public class FunctionImpl
{
//必须是pubic
//方法名称、参数数量、类型、顺序必须和client的接口一致
public int add(int num1, int num2)
{ }
}

build之后会生成FunctionImplBinder,将这个Binder和Service绑定:

public class ServerService extends Service
{
@Override
public IBinder onBind(Intent intent)
{
return new FunctionImplBinder(new FunctionImpl());
}
}

直达Github项目地址

欢迎关注我的博客

还在用AIDL吗?试试EasyMessenger吧的相关教程结束。

《还在用AIDL吗?试试EasyMessenger吧.doc》

下载本文的Word格式文档,以方便收藏与打印。