GitHub - 7hens/okbinder: ? just a simple IPC library for android
source link: https://github.com/7hens/okbinder?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.md
OkBinder
OkBinder 是一个轻量级的跨进程通信方案,可以用来替代 AIDL。
OkBinder is a lightweight IPC library that can be used to replace AIDL.
Features
特点 | AIDL | OkBinder |
---|---|---|
实现方式 | AIDL 接口 | 纯 Java/Kotlin 接口 |
获取方法的返回值 | 支持 | 支持 |
非阻塞式调用 | 使用 oneway | 返回值类型使用 void |
通过参数传值 | 使用 in | 默认支持 |
通过参数取值 | 使用 out | 不支持 |
通过参数传值并取值 | 使用 inout | 不支持 |
通过参数回调 | 支持 | 支持 |
异常日志 | 不完整 | 完整 |
IDE 智能提示 | 较少(需手动 import) | 完整(因为是纯 Java 代码) |
重构代价 | 麻烦(需要重新 build) | 简单(无需 build) |
Setting up the dependency
implementation 'com.github.7hens:okbinder:1.1'
Sample usage
首先,你需要定义一个用于 IPC 的服务接口。这个接口的功能类似于 AIDL,但不同的是,这是一个纯 Java 的接口,并且需要使用 @OkBinder.Interface
注解。
First, you need to define a service interface for IPC. Similar to AIDL, but the difference is that this is a pure Java interface and needs to be annotated with @OkBinder.Interface
.
@OkBinder.Interface public interface IRemoteService { void doSomething(int aInt, IRemoteService callback); }
其次,在服务端,你需要实现上面的服务接口,并用它来创建一个 Binder 供客户端调用。
Second, on the server side, you need to implement the above service interface and use it to create a Binder to provide to the client.
public class MyService extends Service { private Binder okBinder = OkBinder.create(new IRemoteService() { @Override public void doSomething(int aInt, IRemoteService callback) { // pass } }) @Override public IBinder onBind(Intent intent) { return okBinder; } }
最后,你可以使用客户端的 IBinder 创建出一个服务接口的代理对象。使用这个代理,就可以进行跨进程通信了。
Finally, you can use the client's IBinder to create a proxy object for the service interface. Use this proxy to communicate across processes.
public class MyActivity extends Activity implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { IRemoteService remoteService = OkBinder.proxy(IRemoteService.java, service); remoteService.doSomething(0, remoteService); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); bindService(new Intent(this, MyService.java), this, Context.BIND_AUTO_CREATE); } }
最后的最后,OkBinder 真的很轻量哦,只有一个类。你完全可以直接把它(OkBinder.java)拷贝到你的代码里。喜欢的话,记得 star 哦。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK