直達Github項目地址java
一款用於Android平臺的基於Binder的進程間通訊庫,採用annotationProcessor
生成IPC通訊須要的代碼。EasyMessenger
相對於AIDL
具有以下優點:git
EasyMessenger
目前支持以下數據類型:github
implementation 'cn.zmy:easymessenger-lib:0.1'
annotationProcessor 'cn.zmy:easymessenger-compilier:0.1'
複製代碼
聲明接口:bash
@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)
{
//調用失敗
}
});
複製代碼
實現接口:ide
@BinderServer
public class FunctionImpl
{
//必須是pubic
//方法名稱、參數數量、類型、順序必須和client的接口一致
public int add(int num1, int num2)
{
}
}
複製代碼
build以後會生成FunctionImplBinder
,將這個Binder和Service綁定:ui
public class ServerService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new FunctionImplBinder(new FunctionImpl());
}
}
複製代碼
直達Github項目地址spa
歡迎關注個人博客code