### RouterRegex Annotation Example 2: SystemBrowserHandler Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example of using RouterRegex to open other http(s) links with the system browser. ```java @RouterRegex(regex = "http(s)?://.*", priority = 1) public class SystemBrowserHandler extends UriHandler { @Override protected boolean shouldHandle(@NonNull UriRequest request) { return true; } @Override protected void handleInternal(@NonNull UriRequest request, @NonNull UriCallback callback) { try { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(request.getUri()); request.getContext().startActivity(intent); callback.onComplete(UriResult.CODE_SUCCESS); } catch (Exception e) { callback.onComplete(UriResult.CODE_ERROR); } } } ``` -------------------------------- ### StartActivityForResult Callback Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example of setting up a startActivityForResult callback using RouterComponents and DefaultUriRequest. ```Java // application下设置 RouterComponents.setActivityLauncher(ForResultActivityLauncher.Companion.getINSTANCE()); new DefaultUriRequest(this, uri) // 标记这是一个startActivityForResult .activityRequestCode(100) .onComplete(new OnCompleteListener() { @Override public void onSuccess(@NonNull UriRequest request) { ToastUtils.showToast(request.getContext(), "跳转成功"); } @Override public void onError(@NonNull UriRequest request, int resultCode) { ToastUtils.showToast(request.getContext(), "跳转失败"); } }).start(); ``` -------------------------------- ### RouterRegex Annotation Example 1: WebViewActivity Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example of using RouterRegex to open specific http(s) links with WebViewActivity. ```java @RouterRegex(regex = "http(s)?://(.*\\.)?(meituan|sankuai|dianping)\\.(com|info|cn).*") public class WebViewActivity extends BaseActivity { } ``` -------------------------------- ### RouterPage Annotation Example Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example demonstrating RouterPage for internal page jumps, showing both RouterUri and RouterPage configurations and their respective jump methods. ```java // demo://demo/account @RouterUri(path = "/account", scheme = "demo", host = "demo") // wm_router://page/account @RouterPage(path = "/account") public class AccountActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (PageAnnotationHandler.isPageJump(getIntent())) { // ... } else { // ... } } } ``` ```java // 对应RouterUri的配置 Router.startUri(context, "demo://demo/account"); ``` ```java // 对应RouterPage的配置 Router.startUri(context, PageAnnotationHandler.SCHEME_HOST + "/account"); // 或直接用常量的值 Router.startUri(context, "wm_router://page/account"); ``` -------------------------------- ### RouterService Annotation Example Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Demonstrates how to declare implementation classes for RouterService, including interfaces, keys, and singleton settings. ```java public interface IService { } @RouterService(interfaces = IService.class, key = 'key1') public static class ServiceImpl1 implements IService { } @RouterService(interfaces = IService.class, key = 'key2', singleton = true) public static class ServiceImpl2 implements IService { } ``` -------------------------------- ### RouterUri Annotation Example 2: Multiple Paths Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example of a page configured with multiple paths using RouterUri. ```java @RouterUri(scheme = "demo_scheme", host = "demo_host", path = {"/path1"", "/path2"}) public class TestActivity extends Activity { } ``` ```java Router.startUri(context, "demo_scheme://demo_host/path1"); Router.startUri(context, "demo_scheme://demo_host/path2"); ``` -------------------------------- ### RouterUri Annotation Example 3: ABTest Strategy Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example of using RouterUri to handle ABTest strategies for different page redirections. ```java @RouterUri(path = "/home") public class HomeABTestHandler extends AbsActivityHandler { @NonNull @Override protected Intent createIntent(@NonNull UriRequest request) { if (FakeABTestService.getHomeABStrategy().equals("A")) { return new Intent(request.getContext(), HomeActivityA.class); } else { return new Intent(request.getContext(), HomeActivityB.class); } } } ``` ```java Router.startUri(context, "/home"); ``` -------------------------------- ### Lazy Initialization Example Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Illustrates how to perform lazy initialization of WMRouter components in a background thread to improve app startup performance. ```java void initRouter(Context context) { // 必选,需要在主线程执行 Router.init(new DefaultRootUriHandler(context)); // 其他各种配置 // ... // 后台线程懒加载 new AsyncTask() { @Override protected Void doInBackground(Void[] objects) { Router.lazyInit(); return null; } }.execute(); } ``` -------------------------------- ### Custom RootUriHandler and UriRequest Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example demonstrating how to extend WMRouter by creating custom RootUriHandler and UriRequest classes, and then initializing and using them. ```java public class CustomRootUriHandler extends RootUriHandler { // ... public CustomRootUriHandler() { // 添加Uri注解支持 addHandler(new UriAnnotationHandler()); // 添加一个自定义的HttpHandler addHandler(new CustomHttpHandler()); } } // 自定义UriRequest public class CustomUriRequest extends UriRequest { // ... public CustomUriRequest setCustomProperties(String s) { putField("custom_properties", s); return this; } } // 初始化 Router.init(new CustomRootUriHandler()); // 启动Uri CustomUriRequest request = new CustomUriRequest(mContext, url) .setCustomProperties("xxx"); Router.startUri(request); ``` -------------------------------- ### RouterUri Annotation Example 1: User Account Page Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example of using RouterUri for a user account page with a LoginInterceptor. ```java @RouterUri(path = "/account", interceptors = LoginInterceptor.class) public class UserAccountActivity extends Activity { } ``` ```java Router.startUri(context, "/account"); ``` -------------------------------- ### Manifest Configuration - Target Activity Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Example of an activity declaration in the Manifest. Target activities do not require IntentFilter or exported configuration. ```xml ``` -------------------------------- ### Get Service Instance with Context Parameter Constructor Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Retrieves a service instance using its interface, key, and a Context object, utilizing a constructor that accepts Context. ```java // 使用Context参数构造 IService service = Router.getService(IService.class, "key1", context); List list = Router.getAllServices(IService.class, context); ``` -------------------------------- ### Get Service Instance with No-Argument Constructor Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Retrieves a service instance using its interface and key, utilizing a no-argument constructor. ```java // 使用无参构造函数 IService service = Router.getService(IService.class, "key1"); List list = Router.getAllServices(IService.class); ``` -------------------------------- ### LoginInterceptor Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md An example of a LoginInterceptor that checks if the user is logged in before proceeding with the URI jump. ```java public class LoginInterceptor implements UriInterceptor { @Override public void intercept(@NonNull UriRequest request, @NonNull final UriCallback callback) { final FakeAccountService accountService = FakeAccountService.getInstance(); if (accountService.isLogin()) { // 已经登录,不需处理,继续跳转流程 callback.onNext(); } else { // 没登录,提示登录并启动登录页 Toast.makeText(request.getContext(), "请先登录~", Toast.LENGTH_SHORT).show(); accountService.registerObserver(new FakeAccountService.Observer() { @Override public void onLoginSuccess() { accountService.unregisterObserver(this); // 登录成功,继续跳转 callback.onNext(); } @Override public void onLoginFailure() { accountService.unregisterObserver(this); // 登录失败,终止流程,返回错误ResultCode callback.onComplete(CustomUriResult.CODE_LOGIN_FAILURE); } }); // 启动登录页 startActivity(request.getContext(), LoginActivity.class); } } } ``` -------------------------------- ### DemoUriHandler Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md An example of a UriHandler that only handles HTTP links. ```java public interface UriCallback { /** * 处理完成,继续后续流程。 */ void onNext(); /** * 处理完成,终止分发流程。 * * @param resultCode 结果 */ void onComplete(int resultCode); } public class DemoUriHandler extends UriHandler { public void handle(@NonNull final UriRequest request, @NonNull final UriCallback callback) { Uri uri = request.getUri(); // 处理HTTP链接 if ("http".equalsIgnoreCase(uri.getScheme())) { try { // 调用系统浏览器 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); request.getContext().startActivity(intent); // 跳转成功 callback.onComplete(UriResult.CODE_SUCCESS); } catch (Exception e) { // 跳转失败 callback.onComplete(UriResult.CODE_ERROR); } } else { // 非HTTP链接不处理,继续分发 callback.onNext(); } } // ... } ``` -------------------------------- ### Get All Service Classes by Interface Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Retrieves a list of all declared service implementation Classes for a given interface. ```java List> classes = Router.getAllServiceClasses(IService.class); ``` -------------------------------- ### Custom Logger Configuration Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Provides an example of configuring a custom logger for WMRouter's Debugger to handle error reporting and logging. ```java // 自定义Logger DefaultLogger logger = new DefaultLogger() { @Override protected void handleError(Throwable t) { super.handleError(t); // 此处上报Fatal级别的异常 } }; // 设置Logger Debugger.setLogger(logger); // Log开关,建议测试环境下开启,方便排查问题。 Debugger.setEnableLog(true); // 调试开关,建议测试环境下开启。调试模式下,严重问题直接抛异常,及时暴漏出来。 Debugger.setEnableDebug(true); ``` -------------------------------- ### Get Service Instance with Custom Factory Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Retrieves a service instance using a custom Factory for reflection-based construction, suitable for special constructor requirements. ```java // 使用自定义Factory IFactory factory = new IFactory() { public Object create(Class clazz) { return clazz.getConstructor().newInstance(); } }; IService service = Router.getService(IService.class, "key1", factory); List list = Router.getAllServices(IService.class, factory); ``` -------------------------------- ### Get Service Class by Interface and Key Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Retrieves the Class of a specific service implementation using its interface and key, requiring the key to be specified in the annotation. ```java Class clazz = Router.getServiceClass(IService.class, "key1"); ``` -------------------------------- ### Initialization Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Initialize WMRouter in the Application.onCreate() method. ```java // 创建RootHandler DefaultRootUriHandler rootHandler = new DefaultRootUriHandler(context); // 初始化 Router.init(rootHandler); ``` -------------------------------- ### Proguard Configuration - Default Rules Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Default Proguard rules provided by WMRouter. ```bash # 保留ServiceLoaderInit类,需要反射调用 -keep class com.sankuai.waimai.router.generated.ServiceLoaderInit { *; } # 避免注解在shrink阶段就被移除,导致obfuscate阶段注解失效、实现类仍然被混淆 -keep @interface com.sankuai.waimai.router.annotation.RouterService ``` -------------------------------- ### Service Instance Retrieval with Provider Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Demonstrates how to use a static method annotated with @RouterProvider to provide a service instance, which is prioritized over default construction. ```java @RouterService(interfaces = IService.class, key = 'key', singleton = true) public static class ServiceImpl implements IService { public static final ServiceImpl INSTANCE = new ServiceImpl(); // 使用注解声明该方法是一个Provider @RouterProvider public static ServiceImpl provideInstance() { return INSTANCE; } } // 调用时不传Factory,优先找Provider,找不到再使用无参数构造 IService service = Router.getService(IService.class, "key"); List list = Router.getAllServices(IService.class); ``` -------------------------------- ### Initiating URI Jump - Simple Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Initiate a URI jump using the Router.startUri method with context and URI string. ```java // 直接传context和URI Router.startUri(context, "/account"); // 或构造一个UriRequest Router.startUri(new UriRequest(context, "/account")) ``` -------------------------------- ### Gradle Configuration - Annotation Processor for Java Modules Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Configure the annotation processor for Java modules in projects using annotations. ```groovy repositories { jcenter() } dependencies { annotationProcessor 'com.sankuai.waimai.router:compiler:1.x' } ``` -------------------------------- ### Gradle Configuration - Annotation Processor for Kotlin Modules Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Configure the annotation processor for Kotlin modules, including applying the kotlin-kapt plugin. ```groovy apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' // 添加kapt插件 apply plugin: 'kotlin-kapt' repositories { jcenter() } dependencies { kapt 'com.sankuai.waimai.router:compiler:1.x' } ``` -------------------------------- ### Gradle Configuration - Root build.gradle for Plugin Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Configure the WMRouter plugin in the root build.gradle file. ```groovy buildscript { repositories { jcenter() } dependencies { // Android Gradle插件 classpath 'com.android.tools.build:gradle:3.2.1' // 添加WMRouter插件 classpath "com.sankuai.waimai.router:plugin:1.x" } } ``` -------------------------------- ### Gradle Configuration - Application build.gradle for Plugin Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Apply the WMRouter plugin in the application module's build.gradle file. ```groovy apply plugin: 'com.android.application' // 应用WMRouter插件 apply plugin: 'WMRouter' ``` -------------------------------- ### UriProxyActivity Implementation Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Implementation of the UriProxyActivity to handle URI jumps initiated from external sources. ```java public class UriProxyActivity extends BaseActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); DefaultUriRequest.startFromProxyActivity(this, new OnCompleteListener() { @Override public void onSuccess(@NonNull UriRequest request) { finish(); } @Override public void onError(@NonNull UriRequest request, int resultCode) { finish(); } }); } } ``` -------------------------------- ### Proguard Configuration - RouterService Annotation Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Proguard configuration for classes using the @RouterService annotation to avoid issues with reflection. ```bash # 使用了RouterService注解的实现类,需要避免Proguard把构造方法、方法等成员移除(shrink)或混淆(obfuscate),导致无法反射调用。实现类的类名可以混淆。 -keepclassmembers @com.sankuai.waimai.router.annotation.RouterService class * { *; } ``` -------------------------------- ### Manifest Configuration - UriProxyActivity Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Configuration for a proxy activity to handle external URI jumps, including an intent filter for a specific scheme. ```xml ``` -------------------------------- ### Method Invocation with Function Interface Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Shows how to declare and invoke methods using WMRouter's Function interfaces (Func0-FuncN). ```java @RouterService(interfaces = Func2.class, key = "/add", singleton = true) public class AddMethod implements Func2 { @Override public Integer call(Integer a, Integer b) { return a + b; } } ``` ```java Func2 addMethod = Router.getService(Func2.class, "/add"); Integer result = addMethod.call(1, 2); ``` ```java Integer result = Router.callMethod("/add", 1, 2); ``` -------------------------------- ### Initiating URI Jump - With DefaultUriRequest Builder Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Initiate a URI jump using DefaultUriRequest builder to set various parameters like request code, source, intent extras, and animations. ```java new DefaultUriRequest(context, "/account") // startActivityForResult使用的RequestCode .activityRequestCode(100) // 设置跳转来源,默认为内部跳转,还可以是来自WebView、来自Push通知等。 // 目标Activity可通过UriSourceTools区分跳转来源。 .from(UriSourceTools.FROM_INTERNAL) // Intent加参数 .putIntentExtra("test-int", 1) .putIntentExtra("test-string", "str") // 设置Activity跳转动画 .overridePendingTransition(R.anim.enter_activity, R.anim.exit_activity) // 监听跳转完成事件 .onComplete(new OnCompleteListener() { @Override public void onSuccess(@NonNull UriRequest request) { ToastUtils.showToast(request.getContext(), "跳转成功"); } @Override public void onError(@NonNull UriRequest request, int resultCode) { } }) // 这里的start实际也是调用了Router.startUri方法 .start(); ``` -------------------------------- ### Gradle Configuration - Base Library Dependency Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Add the WMRouter dependency to the base library. ```groovy repositories { jcenter() } dependencies { compile 'com.sankuai.waimai.router:router:1.x' } ``` -------------------------------- ### Gradle Configuration - Root build.gradle for Plugin (Excluding Android Gradle Plugin) Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Alternative configuration for the root build.gradle to exclude the Android Gradle plugin from being overridden by WMRouter. ```groovy classpath("com.sankuai.waimai.router:plugin:1.x") { exclude group: 'com.android.tools.build' } ``` -------------------------------- ### Dependency Version Conflict Resolution Source: https://github.com/meituan/wmrouter/blob/master/docs/user-manual.md Gradle configuration to resolve class duplication errors by uniformly replacing older WMRouter dependencies with newer ones. ```gradle allprojects { configurations.all { Configuration c -> resolutionStrategy.eachDependency { DependencyResolveDetails details -> if (details.requested.group == 'com.sankuai.waimai.router') { details.useTarget group: 'io.github.meituan-dianping' } } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.