概述
- Android进程间通信IPC框架
- 像调用本地函数一样调用其他进程的函数
- 在本地进程创建其他进程类的对象
- 在本进程获取其他进程的单例
- 在本进程使用其他进程的工具类
- 支持进程间函数回调,调用其他进程函数的时候可以传入回调函数,让其他进程回调本进程的方法
- 自带内存优化,内置两个垃圾回收器,地进程在远端进程创建的实例和本地进程传给远端进程的回调接口会被自动回收。
基本使用
compile 'xiaofei.library:hermes:0.7.0'
- 1
- 1
两个进程共享一个对象单例
//在进程A中,类使用注解标记该类的id @ClassId(“Singleton”) public class Singleton { //属性 private volatile String mData;
<span class="token comment">//实现单例,代码省略</span> <span class="token keyword">public</span> <span class="token keyword">static</span> Singleton <span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//方法使用注解标记该方法的id</span> <span class="token annotation punctuation">@MethodId</span><span class="token punctuation">(</span>“setData”<span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setData</span><span class="token punctuation">(</span>String data<span class="token punctuation">)</span> <span class="token punctuation">{</span> mData <span class="token operator">=</span> data<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@MethodId</span><span class="token punctuation">(</span>“getData”<span class="token punctuation">)</span> <span class="token keyword">public</span> String <span class="token function">getData</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> mData<span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5