美文网首页
Aroute之进阶使用

Aroute之进阶使用

作者: Method | 来源:发表于2021-07-03 18:22 被阅读0次

    通过Scheme跳转

    Scheme是界面跳转协议,类似URL地址,通过url可以打开某个网站,而通过Scheme可以打开app的某个界面。常用在推送、H5跳转到APP某个界面

    Aroute也是支持Scheme跳转。

    使用

    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    
    <body>
    
    <h2>跳转测试</h2>
    
    <h2>自定义Scheme[通常来说都是这样的]</h2>
    <p><a href="arouter://m.aliyun.com/main/SchemeToAActivity">arouter://m.aliyun.com/test/activity1</a></p>
    <p><a href="arouter://m.aliyun.com/main/SchemeToAActivity?url=https%3a%2f%2fm.abc.com%3fa%3db%26c%3dd">测试URL Encode情况</a></p>
    <p><a href="arouter://m.aliyun.com/main/SchemeToAActivity?name=alex&age=18&boy=true&high=180&obj=%7b%22name%22%3a%22jack%22%2c%22id%22%3a666%7d">arouter://m.aliyun.com/test/activity1?name=alex&age=18&boy=true&high=180&obj={"name":"jack","id":"666"}</a></p>
    </body>
    </html>
    

    配置Scheme协议类

     <activity android:name=".ui.SchemeActivity" >
    
                <!-- Scheme -->
                <intent-filter>
                    <data
                        android:host="m.aliyun.com"
                        android:scheme="arouter"/>
    
                    <action android:name="android.intent.action.VIEW"/>
    
                    <category android:name="android.intent.category.DEFAULT"/>
                    <category android:name="android.intent.category.BROWSABLE"/>
                </intent-filter>
    
                <!-- App Links -->
                <intent-filter android:autoVerify="true">
                    <action android:name="android.intent.action.VIEW"/>
    
                    <category android:name="android.intent.category.DEFAULT"/>
                    <category android:name="android.intent.category.BROWSABLE"/>
    
                    <data
                        android:host="m.aliyun.com"
                        android:scheme="http"/>
                    <data
                        android:host="m.aliyun.com"
                        android:scheme="https"/>
                </intent-filter>
            </activity>
    

    获取Scheme内容通过Aroute跳转

    class SchemeActivity:AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val uri = intent.data
            ARouter.getInstance().build(uri).navigation(this,object : NavCallback(){
                override fun onArrival(postcard: Postcard?) {
                    println(postcard.toString())
                }
            })
            finish()
        }
    }
    

    目标页中获取参数

     @JvmField
     @Autowired(desc = "姓名")
     var name = "jack"
     @JvmField
     @Autowired
     var age = 10
     @JvmField
     @Autowired
     var height = 175
     @JvmField
     @Autowired(name = "boy", required = true)
     var girl = false
    

    拦截器

    @Interceptor(priority = 7,name = "测试拦截器")
    class TextInterceptor : IInterceptor {
        override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
            val mDialog by lazy { AlertDialog.Builder(postcard?.context) }
            if (RoutePath.Main.PAGE_INTERCEPTOR_ACTIVITY == postcard?.path) {
    
                mDialog.apply {
                    setTitle("拦截器")
                    setNegativeButton("继续"
                    ) { _, _ ->
                        callback?.onContinue(postcard)
                    }
    
                    setNeutralButton("算了"){
                        _,_ ->
                        callback?.onInterrupt(Throwable("我觉得有问题"))
                    }
                    setPositiveButton("加点料", object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                            postcard.withString(ArouteKey.MAIN.KEY_DATA, "我是来自拦截器的参数")
                            callback?.onContinue(postcard)
                        }
                    })
    
                }
                Handler(Looper.getMainLooper()).post {
                    mDialog.create().show()
                }
    
            }else{
                callback?.onContinue(postcard)
            }
        }
    
        override fun init(context: Context?) {
            "${TextInterceptor::class.java.simpleName} has init".e()
        }
    }
    

    多个拦截器会按优先级顺序依次执行,priority越小先执行

    callback?.onContinue(postcard) 继续执行
    callback?.onInterrupt(Throwable("我觉得有问题")) 中断路由流程,并回调callback#onInterrupt()
    
    // 以上两种至少需要调用其中一种,否则不会继续路由
    
    postcard.withString(ArouteKey.MAIN.KEY_DATA, "我是来自拦截器的参数")
    postcard.path = ...
    可以添加参数,或者重新设置path
    

    依赖注入

     ARouter.getInstance().build("/test/activity1")
                            .withString("name", "老王")
                            .withInt("age", 18)
                            .withBoolean("boy", true)
                            .withLong("high", 180)
                            .withString("url", "https://a.b.c")
                            .withSerializable("ser", testSerializable)
                            .withParcelable("pac", testParcelable)
                            .withObject("obj", testObj)
                            .withObject("objList", objList)
                            .withObject("map", map)
                            .navigation();
    

    目标类通过依赖获取data

    //name = key  当值是null可能会carsh 
    @Autowired(name = "boy", required = true)
    boolean girl;
    
    @Autowired
    TestSerializable ser;
    
    @Autowired
    TestParcelable pac;
    
    @Autowired
    TestObj obj;
    
    @Autowired
    List<TestObj> objList;
    
    @Autowired
    Map<String, List<TestObj>> map;
    
    

    注意序列化和obj

    public class TestSerializable implements Serializable {
        public String name;
        public int id;
    
        public TestSerializable() {
        }
    
        public TestSerializable(String name, int id) {
            this.name = name;
            this.id = id;
        }
    }
    
    public class TestObj {
        public String name;
        public int id;
    
        public TestObj() {
        }
    
        public TestObj(String name, int id) {
            this.name = name;
            this.id = id;
        }
    }
    
    
    
    // 如果需要传递自定义对象,新建一个类(并非自定义对象类),然后实现 SerializationService,并使用@Route注解标注(方便用户自行选择序列化方式),例如:
    @Route(path = "/yourservicegroupname/json")
    public class JsonServiceImpl implements SerializationService {
        @Override
        public void init(Context context) {
    
        }
    
        @Override
        public <T> T json2Object(String text, Class<T> clazz) {
            return JSON.parseObject(text, clazz);
        }
    
        @Override
        public String object2Json(Object instance) {
            return JSON.toJSONString(instance);
        }
    }
    

    模块间方法调用

    购物车模块想要判断用户是否登录,并且模块间没有依赖。就可以通过暴漏接口获取

    Module_base

    // 声明接口,其他组件通过接口来调用服务
    public interface HelloService extends IProvider {
        String sayHello(String name);
    }
    

    ModuleA

    // 实现接口
    @Route(path = "/yourservicegroupname/hello", name = "测试服务")
    public class HelloServiceImpl implements HelloService {
    
        @Override
        public String sayHello(String name) {
        return "hello, " + name;
        }
    
        @Override
        public void init(Context context) {
    
        }
    }
    

    ModuleB

    public class Test {
        @Autowired
        HelloService helloService;
    
        @Autowired(name = "/yourservicegroupname/hello")
        HelloService helloService2;
    
        HelloService helloService3;
    
        HelloService helloService4;
    
        public Test() {
        ARouter.getInstance().inject(this);
        }
    
        public void testService() {
        // 1. (推荐)使用依赖注入的方式发现服务,通过注解标注字段,即可使用,无需主动获取
        // Autowired注解中标注name之后,将会使用byName的方式注入对应的字段,不设置name属性,会默认使用byType的方式发现服务(当同一接口有多个实现的时候,必须使用byName的方式发现服务)
        helloService.sayHello("Vergil");
        helloService2.sayHello("Vergil");
    
        // 2. 使用依赖查找的方式发现服务,主动去发现服务并使用,下面两种方式分别是byName和byType
        helloService3 = ARouter.getInstance().navigation(HelloService.class);
        helloService4 = (HelloService) ARouter.getInstance().build("/yourservicegroupname/hello").navigation();
        helloService3.sayHello("Vergil");
        helloService4.sayHello("Vergil");
        }
    }
    

    跳转回调

     ARouter.getInstance().build("/xxx/xxx").navigation(this, new NavCallback() {
                        @Override
                        public void onFound(Postcard postcard) {
                            Log.d("ARouter", "找到了");
                        }
    
                        @Override
                        public void onLost(Postcard postcard) {
                            Log.d("ARouter", "找不到了");
                        }
    
                        @Override
                        public void onArrival(Postcard postcard) {
                            Log.d("ARouter", "跳转完了");
                        }
    
                        @Override
                        public void onInterrupt(Postcard postcard) {
                            Log.d("ARouter", "被拦截了");
                        }
                    });
    

    demo地址

    相关文章

      网友评论

          本文标题:Aroute之进阶使用

          本文链接:https://www.haomeiwen.com/subject/uscpultx.html