Android 路由框架

先说一下为什么想写一个这样的框架,Android提供了Intent机制来启动Activity,支持显式跳转和隐式跳转,这已经是一种耦合性低的设计。但是当项目大了,跳转的来源就会很多,有原生的跳转、有H5的跳转、有推送的跳转等等,管理起来就会很麻烦,所以能不能统一这些跳转,为每个Activity配置一个URL,根据schema来判断是要往WebView跳还是要往原生界面跳。

有时候跳转之前还需要进行一些逻辑的判断,比如判断有没有登陆,没有登陆先去登陆,登陆成功之后再跳转到着陆页,还有一些复杂的判断,比如在需要跳转到每个产品购买页之前需要先判断请求一下网络,获取当前用户卡上余额,如果余额不足要先跳转到充值页面,这样一来每个要跳转到产品购买页的地方都要去写相同的代码逻辑,假如能给产品购买页配置一个拦截器,所有跳转到购买页时都先进行拦截去判断当前用户卡上余额,这样就只需要在拦截器的地方写一次就可以了,假如这时新增了一个产品,只需要在该产品的购买页配置上该拦截器就可以,既便于维护也便于扩展。

当然目前这样的框架已经很多,逻辑并不是很复杂,所以自己写了一个,能更适合目前的项目。

@ActivityMapping

第一步,我们需要为每个Activity配置一个唯一标示URL,可以通过配置文件的方式也可以采用注解的方式,记得SpringMVC里面为Controler配置URL的注解是RequestMapping,所以我自定义了一个ActivityMapping注解,通过该注解就可以为Activity配置一个唯一标示URL,还可以指明该Activity需要接受哪些参数。


@ActivityMapping(value = {"product/detail"}, stringParams = {"id"})
public class ProductDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          TextView t = new TextView(this);
          t.setTextColor(Color.BLACK);
          t.setText("product detail id = " + getIntent().getStringExtra("id"));
          setContentView(t);
   }

}

@ActivityInterceptor

第二步,为某些Activity配置拦截器,当然可以是多个拦截器,使用ActivityIntercetor注解,例如之后登陆用户才能进入产品购买页


@ActivityMapping(value = {"product/buy"})
@ActivityInterceptor({LoginInterceptor.class})
public class ProductBuyActivity extends AppCompatActivity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                TextView t = new TextView(this);
                t.setTextColor(Color.BLACK);
                t.setText("product buy");
                setContentView(t);
      }

}

多个拦截器的配置,比如只有登陆并且开户的用户才能进入产品购买页


@ActivityMapping(value = {"product/buy"})
@ActivityInterceptor({LoginInterceptor.class,CheckUserInterceptor.java})
public class ProductBuyActivity extends AppCompatActivity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView t = new TextView(this);
        t.setTextColor(Color.BLACK);
        t.setText("product buy");
        setContentView(t);
    }

}

LoginIntercetor.java


public class LoginInterceptor implements Interceptor {
        boolean hasLogin;
        @Override
        public void doIntercept(Context context, String url, InterceptorChain interceptorChain) {
            if (hasLogin) {
                    interceptorChain.doIntercept(context, url, interceptorChain);
            } else {
                    interceptorChain.sendMsg(new RouterMsg(Router.MSG_FORWARD, "app://user/login"));
        }

    }

}

使用

build.gradle配置


apply plugin: 'android-apt'
apt{
    arguments{
        mainModule true
        moduleName project.name
        includeModules "${project.name}"
   }
}

初始化路由表


public class App extends PluginApp {
    @Override
    public void onCreate() {
        super.onCreate();
        Routers.init("router");
    }

}

进行跳转


//通过url传递参数
Routers.getRouter(context,"schema://product/detail?id=123").open()

//通过withParam()传递
Routers.getRouter(context,"schema://product/detail").withParam("id","123").open()

github地址:https://github.com/imishx/ActivityRouter.git

    原文作者:TechJ_CN
    原文地址: https://www.jianshu.com/p/c35fc59e030d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞