本文主要是介绍ARouter 与 IProvider服务调用,IInterceptor登陆拦截,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
参考:
xingfu0809 / ArouterLogin : 基于Arouter的登录拦截
IProvider
base模块 : 定义服务接口
import com.alibaba.android.arouter.facade.template.IProvider;public interface IModuleService extends IProvider {/*** 获取用户名** @return 用户名*/String getUserName();}
user模块 : 实现服务接口
import android.content.Context;import com.alibaba.android.arouter.facade.annotation.Route;@Route(path = "/user/user_service")
public class UserServiceImpl implements IModuleService {@Overridepublic String getUserName() {return "xq";}@Overridepublic void init(Context context) {}
}
video模块 : 调用服务
//通过path调用服务
String userName1 = ((IModuleService) ARouter.getInstance().build("/user/user_service").navigation()).getUserName();//通过类名调用服务
String userName2 = ARouter.getInstance().navigation(IModuleService.class).getUserName();
我们还可以在base中处理,然后在调用:
base模块:
import com.alibaba.android.arouter.launcher.ARouter;public class ModuleServiceFinder {public static String getUserName() {IModuleService iModuleService = ARouter.getInstance().navigation(IModuleService.class);if (iModuleService != null) {return iModuleService.getUserName();}return "";}}
video模块 :调用服务
//通过base调用服务
String userName3 = ModuleServiceFinder.getUserName();
IInterceptor
定义拦截器
import android.content.Context;import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;public class LoginInterceptorImpl implements IInterceptor {@Overridepublic void process(Postcard postcard, InterceptorCallback callback) {boolean login = UserCenter.isLogin();if (login) {//已登陆不拦截callback.onContinue(postcard);} else {String path = postcard.getPath();switch (path) {//不需要登陆直接进入界面case "/video/main":case "/picture/main":callback.onContinue(postcard);break;default://需要登陆拦截callback.onInterrupt(null);break;}}}@Overridepublic void init(Context context) {}
}
ReadActivity : 需要登陆的界面
ARouter.getInstance().build("/read/main").withString("msg","我是传递到read activity的数据").navigation(this, new NavigationCallback() {//找到了@Overridepublic void onFound(Postcard postcard) {}//找不到了@Overridepublic void onLost(Postcard postcard) {}//跳转成功了@Overridepublic void onArrival(Postcard postcard) {}//被拦截了@Overridepublic void onInterrupt(Postcard postcard) {//跳转到登陆,//把参数和path都传递到登陆页,登陆完成后再跳转String path = postcard.getPath();Bundle extras = postcard.getExtras();ARouter.getInstance().build("/login/main").with(extras).withString("path", path).navigation();}});
LoginActivity : 登陆界面
@Autowired
public String path = "";//登陆后
if (!TextUtils.isEmpty(path)) {ARouter.getInstance()//传递过来的路径.build(path)//传递过来的参数.with(getIntent().getExtras()).navigation();
}
finish();
这篇关于ARouter 与 IProvider服务调用,IInterceptor登陆拦截的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!