EasyMessenger

Introduction: 一款用于 Android 平台的基于 Binder 的进程间通信库
More: Author   ReportBugs   OfficialWebsite   
Tags:

一款用于 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(泛型参数只能是简单类型或者 Parcelable)
  • enum(需要实现 parcelable)

下载


Client 和 Server 工程均需引用下面的依赖.

implementation 'cn.zmy:easymessenger-lib:0.3'
annotationProcessor 'cn.zmy:easymessenger-compiler:0.3'

开始使用


Server 端


Server 实现需要提供给 Client 功能,例如:

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

注意,实现类上面标注了@BinderServer注解,表示这个类是一个 Server 的实现。

build 项目之后会生成FunctionImplProvider类,这是一个ContentProvider,其命名规则为:Server 实现类的名称 + Provider。

接下来,需要将生成的ContentProviderAndroidManifest.xml中予以声明:

<provider
    android:authorities="your-authorities"
    android:name="xx.xx.FunctionImplProvider"
    android:exported="true"/>

请记住android:authorities的值,它是 Client 和 Server 之间进行通信的钥匙。

Client 端


Client 只需要照着 Server 的实现,声明同样签名的接口方法即可:

@BinderClient(key = "your-authorities")
public interface ClientInterface
{
    int add(int num1, int num2);
}

其上面标注了@BinderClient注解,表示类是一个 Client 接口。需要注意的是,@BinderClient注解需要设置其key参数的值, 其值即为 Server 项目中ContentProviderandroid:authorities的值。

build 项目之后,会生成ClientInterfaceHelper类,开发者也正是通过这个生成 Helper 类来和 Server 进行 IPC 通信的。Helper 类的命名规则为:Client 接口的名称 + Helper。接下来看一下 Client 如何使用 Helper 发起 IPC 请求。

//使用之前需要初始化,需要传递 application 类型的 context
ClientInterfaceHelper.instance.__init(appContext);

//同步 IPC 示例。在 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)
    {
        //调用失败
    }
});

限制

API Level >= 18

API Level < 18 的项目可以使用:

implementation 'cn.zmy:easymessenger-lib:0.2'
annotationProcessor 'cn.zmy:easymessenger-compiler:0.2'

EasyMessenger目前只支持下面的数据类型:

  • boolean, byte, char, short, int, long, float, double
  • boolean[], byte[], char[], int[], long[], float[], double[]
  • String, String[]
  • Parcelable, Parcelable[]
  • Serializable
  • ArrayList(泛型参数只能是简单类型或者 Parcelable)
  • enum(需要实现 parcelable)

ContentProvider的限制

由于EasyMessenger使用ContentProvider来获取 Server 的 Binder 的代理,而ContentProvider会先于Application#onCreate初始化,所以对于一些初始化代码可能需要放置于Application#attachBaseContext中。

License


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools