欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

还在用AIDL吗?试试EasyMessenger吧

程序员文章站 2022-10-25 08:01:18
EasyMessenger ====== "直达Github项目地址" 一款用于Android平台的基于Binder的进程间通信库,采用 生成IPC通信需要的代码。 相对于 具备如下优势: 采用Java声明接口,更方便 接口方法支持重载 同时支持同步和异步通信 目前支持如下数据类型: boolean ......

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