@zhuhf
2018-01-02T13:01:28.000000Z
字数 2592
阅读 1148
public interface IProcessor {
/**
* @return 可以处理 Action 集合
*/
@NonNull String[] getAction();
/**
* 处理该 scheme
* @param activity
* @param schemeUrl 原 scheme 数据
* @param action
* @param params
* @return true:处理成功
*/
boolean handleScheme(final FragmentActivity activity, final @NonNull String schemeUrl, final @NonNull String action, final Map<String, String> params);
}
实现 IProcessor
接口,定义各个子模块的处理器:
public class TrvSchemeProcessor implements IProcessor {
public static final String ACTION_TRAVEL = "JCY_Travel_trvPage";
@Override
public String[] getAction() {
return new String[]{ACTION_TRAVEL};
}
@Override
public boolean handleScheme(FragmentActivity activity, String schemeUrl, String action, Map<String, String> params) {
String travelId = params.get("travelId");
if (TextUtils.isEmpty(travelId)) {
//旅游首页
ARouter.getInstance().build(IntentPath.TRAVEL_HOME)
.navigation();
} else {
//旅游详情页
ARouter.getInstance().build(IntentPath.TRAVEL_DETAIL)
.withString("travelId", travelId)
.navigation();
}
return true;
}
}
getAction()
方法返回可以处理的 action 数组;handleScheme(...)
方法实现处理逻辑,返回 true 表示已处理该 Scheme,false 表示未处理;public interface ModuleTravelService extends IProvider {
/**
* 验票
* @param context
* @param mobile 手机号
* @param ticketData 二维码数据
*/
void verifyTicket(final Context context, final String mobile, final String ticketData);
}
修改步骤:
extends IProvider
改为 extends SchemeProvider
;服务实现者,重写 onCreate()
方法;
@Route(path = IntentPath.SERVICE_TRAVEL)
public class ModuleTravelServiceImpl implements ModuleTravelService {
@Override
public void init(Context context) {}
@Override
public void onCreate() {
JCScheme.register(TrvSchemeProcessor.class);
}
@Override
public void verifyTicket(final Context context, String mobile, String ticketData) {
// ...
}
}
onCreate()
方法中注册第一步定义的处理器;
JCScheme.register(TrvSchemeProcessor.class);
可以按照已定义服务的步骤去实现,也可以直接省略 ModuleTravelService
接口的定义,直接实现 SchemeProvider
接口:
@Route(path = IntentPath.SERVICE_TRAVEL)
public class ModuleTravelServiceImpl implements SchemeProvider {
@Override
public void init(Context context) {}
@Override
public void onCreate() {
JCScheme.register(TrvSchemeProcessor.class);
}
}
JCScheme#init()
方法中,定义注册处理器的 ARouter
服务路径
public static void init() {
if (!hasInit) {
String[] mProviderPaths = new String[]{IntentPath.SERVICE_TRAVEL};
mProcessorMap = new HashMap<>();
for (String providerPath : mProviderPaths) {
SchemeProvider schemeProvider = (SchemeProvider) ARouter.getInstance().build(providerPath).navigation();
if (schemeProvider != null) {
schemeProvider.onCreate();
}
}
hasInit = true;
}
}
初始化 JCScheme
一般在应用的 Application#onCreate() 方法中初始化:
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JCScheme.init();
// ....
}
}
使用 JCScheme
JCScheme.getInstance().handle((FragmentActivity) mContext, schemeUrl);