1. 添加依赖
android {
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = ["moduleName": project.name]
}
}
}
}
dependencies {
implementation 'com.chenenyu.router:router:1.4.0-beta1'
annotationProcessor 'com.chenenyu.router:compiler:1.4.0-beta1'
}
2. Application中注册
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
Router.initialize(new Configuration.Builder()
// 调试模式,开启后会打印log
.setDebuggable(BuildConfig.DEBUG)
// 注册模块名,每个Router使用的Module都要在这里注册
.registerModules("module1", "module2", "app")
.build());
}
}
3. 跳转URI
/**
* uri 可填写网页链接
* 1. http://www.baidu.com/
* 2. 动态添加路由中map集合中的键
*/
Router.build(uri).callback(new RouteCallback() {
@Override
public void callback(RouteResult state, Uri uri, String message) {
if (state == RouteResult.SUCCEED) {
Toast.makeText(MainActivity.this, "succeed: " + uri.toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "error: " + uri + ", " + message, Toast.LENGTH_SHORT).show();
}
}
}).go(this);
4. 动态添加路由
// 添加动态路由
Router.handleRouteTable(new RouteTable() {
@Override
public void handle(Map> map) {
map.put("dynamic", DynamicActivity.class);
}
});
5. 表过滤器与目标过滤器
Router.handleInterceptorTable(new InterceptorTable() {
@Override
public void handle(Map> map) {
Log.e(TAG, "handle: " + map.toString());
}
});
Router.handleTargetInterceptors(new TargetInterceptors() {
@Override
public void handle(Map, String[]> map) {
Log.e(TAG, "handle: " + map.toString());
}
});
6. 跳转路由
Router.build("dynamic").go(this);
7. 注解注册
// 注解注册
@Route("result")
public class ForResultActivity extends AppCompatActivity {}
// 使用Bundle传递数据
Router.build("result").requestCode(0).with("extra", "Bundle From Module1Activity").go(this);
8. 自定义过滤器
// 1. 自定义过滤器
@Interceptor("SampleInterceptor")
public class SampleInterceptor implements RouteInterceptor {
@Override
public boolean intercept(Context context, RouteRequest routeRequest) {
Toast.makeText(context, String.format("Intercepted: {uri: %s, interceptor: %s}",
routeRequest.getUri().toString(), SampleInterceptor.class.getName()), Toast.LENGTH_SHORT).show();
return true;
}
}
// 2. 使用
@Route(value = "intercepted", interceptors = "SampleInterceptor")
public class InterceptedActivity extends AppCompatActivity {}
// 3. 添加过滤器
Router.build("test").addInterceptors("SampleInterceptor").go(this);
// 4. 跳过过滤器Router.build("intercepted").skipInterceptors("SampleInterceptor").go(this);
9. 协议过滤
// 1. AndroidManifest.xml文件配置
android:host="filter"
android:scheme="router" />
// 2. 协议过滤
public class SchemeFilterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scheme_filter);
Uri uri = getIntent().getData();
if (null != uri) {
Toast.makeText(this, "uri: " + uri.toString(), Toast.LENGTH_SHORT).show();
if (!uri.toString().startsWith("router://filter")) {
Router.build(uri).go(this);
}
finish();
}
}
}
// 3. 使用
Router.build("router://filter").go(this);