Android路由框架AnnoRouter:使用Java接口来定义路由跳转

介绍

AnnoRouter是一个用于协助Android组件化的路由框架。它参考了Retrofit的接口设计,将路由跳转API转化为Java接口,使用注解来配置跳转信息。

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&param2=1 将会匹配上方法 jumpToActivity2
  • scheme://host/path?param1=a&param2=1&param3=1 将会匹配上方法 jumpToActivity1, 但 param2=1&param3=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交流 github.com/EastWoodYan…

转载于:https://juejin.im/post/5b57b5115188251b3d79d54e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值