Skip to content

Commit de252e9

Browse files
committed
Add Chinese README_CN.md.
1 parent afd5ae3 commit de252e9

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
[![JCenter](https://api.bintray.com/packages/codezjx/maven/and-linker/images/download.svg)](https://bintray.com/codezjx/maven/and-linker/_latestVersion)
44
[![License](https://img.shields.io/badge/License-Apache%20License%202.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)
55

6+
# AndLinker
7+
8+
[中文文档](README_CN.md)
9+
610
## Introduction
711

812
AndLinker is a IPC(Inter-Process Communication) library for Android, which combines the features of [AIDL][aidl] and [Retrofit][retrofit]. Allows IPC call seamlessly compose with [RxJava][rxjava] and [RxJava2][rxjava2] call adapters. Project design and partial code refer to the great project [Retrofit][retrofit].

README_CN.md

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
2+
## 简介
3+
4+
AndLinker是一款Android上的IPC (进程间通信) 库,结合了[AIDL][aidl][Retrofit][retrofit]的诸多特性,且可以与[RxJava][rxjava][RxJava2][rxjava2]的Call Adapters无缝结合使用。项目的设计与部分代码参考了伟大的[Retrofit][retrofit]项目。
5+
6+
## 配置
7+
8+
在项目根目录的`build.gradle`中添加`jcenter()`仓库
9+
```groovy
10+
allprojects {
11+
repositories {
12+
jcenter()
13+
}
14+
}
15+
```
16+
17+
在App的`build.gradle`中添加如下依赖
18+
```groovy
19+
dependencies {
20+
implementation 'com.codezjx.library:andlinker:0.7.0'
21+
}
22+
```
23+
24+
## 功能特性
25+
26+
- 以普通Java接口代替AIDL接口
27+
-[Retrofit][retrofit]一样生成远程服务接口的IPC实现
28+
- 支持的Call Adapters:`Call`[RxJava][rxjava] `Observable`[RxJava2][rxjava2] `Observable` & `Flowable`
29+
- 支持远程服务回调机制
30+
- 支持AIDL的所有数据类型
31+
- 支持AIDL的所有数据定向tag:`in``out``inout`
32+
- 支持AIDL的`oneway`关键字
33+
34+
## 快速开始
35+
36+
使用注解`@ClassName``@MethodName`修饰远程服务接口`IRemoteService`,并实现它
37+
38+
```java
39+
@ClassName("com.example.andlinker.IRemoteService")
40+
public interface IRemoteService {
41+
42+
@MethodName("getPid")
43+
int getPid();
44+
45+
@MethodName("basicTypes")
46+
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
47+
double aDouble, String aString);
48+
}
49+
50+
private final IRemoteService mRemoteService = new IRemoteService() {
51+
52+
@Override
53+
public int getPid() {
54+
return android.os.Process.myPid();
55+
}
56+
57+
@Override
58+
public void basicTypes(int anInt, long aLong, boolean aBoolean,
59+
float aFloat, double aDouble, String aString) {
60+
// Does something
61+
}
62+
};
63+
```
64+
65+
在服务端App中,创建`AndLinkerBinder`对象,并注册上面的接口实现。然后在`onBind()`方法中返回,暴露给客户端
66+
67+
```java
68+
private AndLinkerBinder mLinkerBinder;
69+
70+
public class RemoteService extends Service {
71+
@Override
72+
public void onCreate() {
73+
super.onCreate();
74+
mLinkerBinder = AndLinkerBinder.Factory.newBinder();
75+
mLinkerBinder.registerObject(mRemoteService);
76+
}
77+
78+
@Override
79+
public IBinder onBind(Intent intent) {
80+
return mLinkerBinder;
81+
}
82+
}
83+
```
84+
85+
在客户端App中,通过`Builder`创建`AndLinker`对象,并通过`create()`方法生成一个`IRemoteService`远程接口的IPC实现
86+
87+
```java
88+
public class BindingActivity extends Activity {
89+
90+
private AndLinker mLinker;
91+
private IRemoteService mRemoteService;
92+
93+
@Override
94+
protected void onCreate(@Nullable Bundle savedInstanceState) {
95+
super.onCreate(savedInstanceState);
96+
mLinker = new AndLinker.Builder(this)
97+
.packageName("com.example.andlinker")
98+
.action("com.example.andlinker.REMOTE_SERVICE_ACTION")
99+
.build();
100+
mLinker.bind();
101+
102+
mRemoteService = mLinker.create(IRemoteService.class);
103+
}
104+
105+
@Override
106+
protected void onDestroy() {
107+
super.onDestroy();
108+
mLinker.unbind();
109+
}
110+
}
111+
```
112+
113+
一切就绪,现在`mRemoteService`对象中的所有方法都是IPC方法,直接调用即可
114+
115+
```java
116+
int pid = mRemoteService.getPid();
117+
mRemoteService.basicTypes(1, 2L, true, 3.0f, 4.0d, "str");
118+
```
119+
120+
## 支持数据类型
121+
122+
AndLinker支持AIDL所有数据类型:
123+
- Java语言中的所有原始类型 (如:`int``long``char``boolean`,等等)
124+
- `String`
125+
- `CharSequence`
126+
- `Parcelable`
127+
- `List` (List中的所有元素必须是此列表中支持的数据类型)
128+
- `Map` (Map中的所有元素必须是此列表中支持的数据类型)
129+
130+
## 进阶使用
131+
132+
### Call Adapters
133+
在客户端App中,你可以copy并修改远程服务接口,包装方法的返回值
134+
135+
```java
136+
@ClassName("com.example.andlinker.IRemoteService")
137+
public interface IRemoteService {
138+
139+
@MethodName("getPid")
140+
Observable<Integer> getPid();
141+
142+
@MethodName("basicTypes")
143+
Call<Void> basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
144+
double aDouble, String aString);
145+
}
146+
```
147+
148+
`AndLinker.Builder`中注册对应的Call Adapter Factory,剩下的步骤基本和[Retrofit][retrofit]一致,不再赘述
149+
150+
```java
151+
new AndLinker.Builder(this)
152+
...
153+
.addCallAdapterFactory(OriginalCallAdapterFactory.create()) // Basic
154+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // RxJava2
155+
.build();
156+
```
157+
158+
### 处理远程服务接口回调
159+
使用`@ClassName``@MethodName`注解修饰远程服务回调接口`IRemoteCallback`
160+
161+
```java
162+
@ClassName("com.example.andlinker.IRemoteCallback")
163+
public interface IRemoteCallback {
164+
165+
@MethodName("onValueChange")
166+
void onValueChange(int value);
167+
}
168+
```
169+
170+
在远程方法中使用`@Callback`注解修饰callback参数
171+
172+
```java
173+
@MethodName("registerCallback")
174+
void registerCallback(@Callback IRemoteCallback callback);
175+
```
176+
177+
在客户端App中,实现上面定义的远程服务回调接口`IRemoteCallback`,并且注册到`AndLinker`中,就是这么简单
178+
179+
```java
180+
private final IRemoteCallback mRemoteCallback = new IRemoteCallback() {
181+
@Override
182+
public void onValueChange(int value) {
183+
// Invoke when server side callback
184+
}
185+
};
186+
mLinker.registerObject(mRemoteCallback);
187+
```
188+
189+
### 指定数据定向tag
190+
你可以为远程方法的参数指定`@In``@Out`,或者`@Inout`注解,它标记了数据在底层Binder中的流向,跟AIDL中的用法一致
191+
192+
```java
193+
@MethodName("directionalParamMethod")
194+
void directionalParamMethod(@In KeyEvent event, @Out int[] arr, @Inout Rect rect);
195+
```
196+
197+
>**注意**:
198+
>- 所有非原始类型必须指定数据定向tag:`@In``@Out`,或者`@Inout`,用来标记数据的流向。原始类型默认是`@In`类型,并且不能指定其他值。
199+
>- 使用`@Out`或者`@Inout`修饰的Parcelable参数必须实现`SuperParcelable`接口,否则你必须手动添加此方法`public void readFromParcel(Parcel in)`
200+
201+
### 使用`@OneWay`注解
202+
你可以在远程方法上使用`@OneWay`注解,这会修改远程方法调用的行为。当使用它时,远程方法调用不会堵塞,它只是简单的发送数据并立即返回,跟AIDL中的用法一致
203+
204+
```java
205+
@MethodName("onewayMethod")
206+
@OneWay
207+
void onewayMethod(String msg);
208+
```
209+
210+
## 反馈
211+
212+
欢迎各位提issues和PRs!
213+
214+
## License
215+
216+
Copyright 2018 codezjx <[email protected]>
217+
218+
Licensed under the Apache License, Version 2.0 (the "License");
219+
you may not use this file except in compliance with the License.
220+
You may obtain a copy of the License at
221+
222+
http://www.apache.org/licenses/LICENSE-2.0
223+
224+
Unless required by applicable law or agreed to in writing, software
225+
distributed under the License is distributed on an "AS IS" BASIS,
226+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
227+
See the License for the specific language governing permissions and
228+
limitations under the License.
229+
230+
231+
[retrofit]: https://github.com/square/retrofit
232+
[rxjava]: https://github.com/ReactiveX/RxJava/tree/1.x
233+
[rxjava2]: https://github.com/ReactiveX/RxJava/tree/2.x
234+
[aidl]: https://developer.android.com/guide/components/aidl.html

0 commit comments

Comments
 (0)