|
1 | 1 | # Android-Router |
2 | | -come soon |
| 2 | +<img src="img/2.png" width = "660" height = "300"/> |
3 | 3 |
|
| 4 | +|lib|androidrouter|androidrouter-compiler|androidrouter-annotations| |
| 5 | +|---|---|---|---| |
| 6 | +|version|[  ](https://bintray.com/tangxiaolv/maven/androidrouter/1.0.0/link)|[  ](https://bintray.com/tangxiaolv/maven/androidrouter-compiler/1.0.0/link)|[  ](https://bintray.com/tangxiaolv/maven/androidrouter-annotations/1.0.0/link)| |
| 7 | +高性能,灵活,简单易用的轻量级Android组件化协议框架,用来解决复杂工程的互相依赖,解耦出的单个模块有利于独立开发和维护。 |
4 | 8 |
|
5 | | -|androidrouter|androidrouter-compiler|androidrouter-annotations| |
6 | | -|---|---|---| |
7 | | -|[  ](https://bintray.com/tangxiaolv/maven/androidrouter/0.0.1/link)|[  ](https://bintray.com/tangxiaolv/maven/androidrouter-compiler/0.0.1/link)|[  ](https://bintray.com/tangxiaolv/maven/androidrouter-annotations/0.0.1/link)| |
| 9 | +目标 |
| 10 | +--- |
| 11 | +- 工程解耦 |
| 12 | +- 模块独立开发独立维护 |
| 13 | +- 让生活变得美好 |
8 | 14 |
|
9 | | -compile 'com.library.tangxiaolv:androidrouter:0.0.1' |
| 15 | +特性 |
| 16 | +--- |
| 17 | +- 编译时注入 |
| 18 | +- 路由过程抛出的异常集中处理 |
| 19 | +- 任意参数类型回传 |
| 20 | +- 运行时动态参数类型解析,支持不同类型传值 |
| 21 | + * From jsonObject => To Object |
| 22 | + * From jsonArray => To List |
| 23 | + * From Object A => To Object A |
| 24 | + * From Object A => To Object B |
| 25 | + * From List< A> => To Object List< B> |
10 | 26 |
|
11 | | -compile 'com.library.tangxiaolv:androidrouter-compiler:0.0.1' |
| 27 | +框架架构图 |
| 28 | +--- |
| 29 | + |
| 30 | +<img src="img/1.png" width = "824" height = "528"/> |
| 31 | + |
| 32 | +Gradle |
| 33 | +--- |
| 34 | +``` |
| 35 | +//需要在各自的application/library 中添加依赖 |
| 36 | +dependencies { |
| 37 | + compile 'com.library.tangxiaolv:androidrouter:1.0.0' |
| 38 | + annotationProcessor 'com.library.tangxiaolv:androidrouter-compiler:1.0.0 |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +快速入门 |
| 43 | +--- |
| 44 | +注:使用本框架需要遵守标准协议格式:scheme://host/path?params=json |
| 45 | +*scheme[1] host[1] path[2] params[2] 1:必须 2:可选* |
| 46 | + |
| 47 | +###第一步:给自定义Module配置注解协议 |
| 48 | +```java |
| 49 | +/** |
| 50 | + * Support parameter types |
| 51 | + * |
| 52 | + * float |
| 53 | + * int |
| 54 | + * long |
| 55 | + * double |
| 56 | + * boolean |
| 57 | + * String |
| 58 | + * List<?> |
| 59 | + * Map<String,Object> |
| 60 | + * custom object |
| 61 | + */ |
| 62 | +@RouterModule(scheme = "android", host = "main") |
| 63 | +public class MainModule implements IRouter { |
| 64 | + |
| 65 | + |
| 66 | + //Route => android://main |
| 67 | + //默认传递Application context, String scheme, VPromise promise |
| 68 | + @RouterPath |
| 69 | + public void def(Application context, String scheme, VPromise promise) { |
| 70 | + promise.resolve("","from scheme: [" + scheme + "] " + "path: []"); |
| 71 | + } |
| 72 | + |
| 73 | + //Route => android://main/activity/localActivity |
| 74 | + @RouterPath("/activity/localActivity") |
| 75 | + public void openLocalActivityAndReturnResult(Application context, VPromise promise) { |
| 76 | + String tag = promise.getTag(); |
| 77 | + Intent intent = new Intent(context, LocalActivity.class); |
| 78 | + intent.putExtra("tag", tag); |
| 79 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 80 | + context.startActivity(intent); |
| 81 | + } |
| 82 | + |
| 83 | + //Take out the value from json object |
| 84 | + //Route => android://main/params/basis?params={'f':1,'i':2,'l':3,'d':4,'b':true} |
| 85 | + @RouterPath("/params/basis") |
| 86 | + public void paramsBasis(float f, int i, long l, double d, boolean b, |
| 87 | + String scheme, VPromise promise) { |
| 88 | + promise.resolve("","from scheme: [" + scheme + "] " + "path: [/params/basis]"); |
| 89 | + } |
| 90 | + |
| 91 | + //Take out the value from json object |
| 92 | + //Route => android://main/params/complex?params={'b':{},'listC':[]} |
| 93 | + @RouterPath("/params/complex") |
| 94 | + public void paramsComplex(B b, List<C> listC, String scheme, VPromise promise) { |
| 95 | + promise.resolve("","from scheme: [" + scheme + "] " + "path: [/params/complex]"); |
| 96 | + } |
| 97 | + |
| 98 | + //from json object => to object |
| 99 | + //key name must be "_params_" |
| 100 | + @RouterPath("/jsonObject") |
| 101 | + public void paramsPakege(Package _params_, String scheme, VPromise promise) { |
| 102 | + promise.resolve("","from scheme: [" + scheme + "] " + "path: [/jsonObject]"); |
| 103 | + } |
| 104 | + |
| 105 | + //from json array => to list |
| 106 | + //key name must be "_params_" |
| 107 | + @RouterPath("/jsonArray") |
| 108 | + public void jsonArray(List<A> _params_, String scheme, VPromise promise) { |
| 109 | + promise.resolve("","from scheme: [" + scheme + "] " + "path: [/jsonArray]"); |
| 110 | + } |
| 111 | + |
| 112 | + //eg: from A => to B |
| 113 | + @RouterPath("/differentTypes") |
| 114 | + public void differentTypes(A a, List<A> listA, String scheme, VPromise promise) { |
| 115 | + promise.resolve("","from scheme: [" + scheme + "] " + "path: [/differentTypes]"); |
| 116 | + } |
| 117 | + |
| 118 | + @RouterPath("/throwError") |
| 119 | + public void throwError(VPromise promise) { |
| 120 | + //返回错误推荐使用RouterRemoteException |
| 121 | + promise.reject(new RouterRemoteException("I'm error.................")); |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | +###第二步:调用协议 |
| 126 | +``` |
| 127 | +//任意地方调用 |
| 128 | +//方式一 |
| 129 | +AndroidRouter.open("android://main/activity/localActivity").call(new Resolve() { |
| 130 | + @Override |
| 131 | + public void call(String type, Object result) { |
| 132 | + //获取返回值 |
| 133 | + } |
| 134 | + }, new Reject() { |
| 135 | + @Override |
| 136 | + public void call(Exception e) { |
| 137 | + //所有路由过程中的异常都会回调到这里 |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | +//方式二 |
| 142 | + AndroidRouter.open("android", "main", "/differentTypes", null) |
| 143 | + .showTime()//开启本次路由调用耗时时间 |
| 144 | + .call();//忽略返回值和错误 |
| 145 | +``` |
| 146 | + |
| 147 | +License |
| 148 | +--- |
| 149 | + Copyright 2017 TangXiaoLv |
| 150 | + |
| 151 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 152 | + you may not use this file except in compliance with the License. |
| 153 | + You may obtain a copy of the License at |
| 154 | + |
| 155 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 156 | + |
| 157 | + Unless required by applicable law or agreed to in writing, software |
| 158 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 159 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 160 | + See the License for the specific language governing permissions and |
| 161 | + limitations under the License. |
0 commit comments