起首须要定义我们本身的注解(篇幅有限,这里只列出声部分源码)。
用于定义跳转 URI 的注解 FullUri:
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface FullUri {
- String value();
- }
用于定义跳转传参的 UriParam( UriParam 注解的参数用于拼接到 URI 后面):
- @Target(ElementType.PARAMETER)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface UriParam {
- String value();
- }
用于定义跳转传参的 IntentExtrasParam( IntentExtrasParam 注解的参数最终经由过程 Intent 来传递):
- @Target(ElementType.PARAMETER)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface IntentExtrasParam {
- String value();
- }
然后实现 Router ,内部经由过程动态代劳的方法来实现 Activity 跳转:
- public final class Router {
- ...
- public <T> T create(final Class<T> service) {
- return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class[]{service}, new InvocationHandler() {
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- FullUri fullUri = method.getAnnotation(FullUri.class);
- StringBuilder urlBuilder = new StringBuilder();
- urlBuilder.append(fullUri.value());
- //获取注浇愁数
- Annotation[][] parameterAnnotations = method.getParameterAnnotations();
- HashMap<String, Object> serializedParams = new HashMap<>();
- //拼接跳转 URI
- int position = 0;
- for (int i = 0; i < parameterAnnotations.length; i++) {
- Annotation[] annotations = parameterAnnotations[i];
推荐阅读
【51CTO.com原创稿件】提到Linux,我们就会想到红帽,就跟提到Windows就会想到微软一样。作为一家专注于架构平>>>详细阅读
本文标题:Android模块化探索与实践
地址:http://www.17bianji.com/lsqh/35300.html
1/2 1