介绍
AnnoRouter是一个用于协助Android组件化的路由框架。它参考了Retrofit的接口设计,将路由跳转API转化为Java接口,使用注解来配置跳转信息。
AnnoRouter示例Usage
初始化全局Router
Router.Builder builder = new Router.Builder()
.application(this)
...
Router.init(builder);
拦截过滤url
new Router.Builder()
.routerUrlFilter(new IRouterUrlFilter() {
@Override
public String filter(String url) {
...
return url;
}
})
...
异常错误处理
new Router.Builder()
.exceptionHandler(new IExceptionHandler() {
@Override
public void handler(String url, Exception e) {
}
})
...
定义路由Api
使用 @RouterScheme
, @RouterHost
, @Path
及 @Param
来定义一个路由地址。
@RouterScheme("scheme")
@RouterHost("host")
public interface RouterApi {
@Path("path")
...
void jump(@Param("paramName") int paramValue);
}
public interface RouterApi {
@RouterScheme("scheme")
@RouterHost("host")
@Path("path")
...
void jump(@Param("paramName") int paramValue);
}
有时候,存在 scheme://host/path
相同但参数不一样的路由地址,就需要使用注解 @Strict
来作区别。
e.g.
@RouterScheme("scheme")
@RouterHost("host")
public interface RouterApi {
@Path("path")
...
void jumpToActivity1(@Param("param1") String param1);
@Strict
@Path("path")
...
void jumpToActivity2(@Param("param1") String param1, @Param("param2") int param2);
}
-
scheme://host/path?param1=a
将会匹配上方法jumpToActivity1
-
scheme://host/path?param1=a¶m2=1
将会匹配上方法jumpToActivity2
-
scheme://host/path?param1=a¶m2=1¶m3=1
将会匹配上方法jumpToActivity1
, 但param2=1¶m3=1
会被忽略掉。
处理前置校验或准备任务
public interface RouterApi {
@Task(CustomRouterTask.class)
...
void jumpToActivity();
}
// ----------------
public class CustomRouterTask implements IRouterTask {
@Override
public void execute(Context context, RouterInfo routerInfo, OnTaskResult onTaskResult) {
// do something...
onTaskResult.success();
}
}
跳转到Activity或自定义处理
public interface RouterApi {
...
@Activity(LoginActivity.class)
void jumpToLogin();
...
@RouterHandler(CustomRouterHandler.class)
void jumpToLogin();
}
// ----------------
public class CustomRouterHandler implements IRouterHandler {
@Override
public void applyRouter(Context context, RouterInfo routerInfo, OnRouterResult routerResult) {
// do what you want to do.
if(routerResult != null) {
routerResult.onSuccess();
}
}
}
自定义Activity转场动画
public interface RouterApi {
...
@Transition(CustomeTransition.class)
void jumpToLogin();
}
// ----------------
public class CustomeTransition implements IActivityTransition {
@Override
public int enterAnim() {
return R.anim.fade_in;
}
@Override
public int exitAnim() {
return R.anim.fade_out;
}
}
设置Activity launchMode
public interface RouterApi {
...
@Flags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
void jump(@Flags int flags);
...
void jump(@Flags int flags);
}
// ----------------
Router.create(RouterApi.class).jump(Intent.FLAG_ACTIVITY_CLEAR_TOP);
添加路由Api
需要将定义好的路由Api添加至Router
。如果只通过接口而不通过url跳转,可以不添加。
Router.addRouterIndex(RouterApi.class);
自定义协议处理
主要处理一些特殊url协议,比如 http
, https
public class HttpSchemeHandler implements ISchemeHandler {
@Override
public void applyRouter(Context context, String url, OnRouterResult routerResult) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(url);
intent.setData(content_url);
context.startActivity(intent);
if(routerResult != null) {
routerResult.onSuccess();
}
}
}
// ----------------
HttpSchemeHandler httpSchemeHandler = new HttpSchemeHandler();
Router.addSchemeHandler("https", httpSchemeHandler);
Router.addSchemeHandler("http", httpSchemeHandler);
使用路由Api进行跳转
两种跳转调用方式:接口跳转,url跳转。
// The Router class generates an implementation of the RouterApi interface.
RouterApi routerApi = Router.create(RouterApi .class);
routerApi.jump("value");
// or use url instead.
Router.execute("scheme://host/path?param=value");
获取Activity Result
@RouterScheme("app")
@RouterHost("usercenter")
public interface LoginRouterApi {
@Path("login")
@Activity(LoginActivity.class)
@RequestCode(1001)
void jumpToLogin(@Param("mobile") String mobile);
@Activity(LoginActivity.class)
@RequestCode(1001)
void jumpToLogin(@Param("mobile") String mobile, OnActivityResult onActivityResult);
}
// ----------------
OnActivityResult onActivityResult = new OnActivityResult() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
@Override
public void onSuccess() {
}
@Override
public void onFailure(Exception e) {
}
};
Router.execute("app://usercenter/login?mobile=0123456789", onActivityResult);
// or
Router.create(LoginRouterApi.class).jumpToLogin("0123456789", onActivityResult);
Get it
AnnoAdapter is now available on JCentral.
implementation 'com.eastwood.common:anno-router:1.0.2'
结语
AnnoRouter已上传Github,欢迎Star交流
https://github.com/EastWoodYang/AnnoRouter
网友评论