您现在的位置: 主页 > 嵌入式操作系统 > Android > 还在用AIDL吗?试试EasyMessenger吧
本文所属标签:
为本文创立个标签吧:

还在用AIDL吗?试试EasyMessenger吧

来源:net 网络用户发布,如有版权联系网管删除 2019-02-27 

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

声明接口:

@BinderClientpublic 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

实现接口:

@BinderServerpublic 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项目地址

欢迎关注我的博客



              查看评论 回复



嵌入式交流网主页 > 嵌入式操作系统 > Android > 还在用AIDL吗?试试EasyMessenger吧
 接口 调用 通信

"还在用AIDL吗?试试EasyMessenger吧"的相关文章

网站地图

围观()