2021 Java反射

2021-10-21  本文已影响0人  咚咚_Coding

为了解耦,在lib中跳转某个路由,所以用反射路由实现

RouterUtils.paySuccessJump(PaymentActivity.this, pay_url, web_router);

make Postcard

public Postcard(String path, String group) {
    this(path, group, null, null);
}

public Postcard withString(@Nullable String key, @Nullable String value) {
    mBundle.putString(key, value);
    return this;
}

start

public static void paySuccessJump(Activity activity, String app_success_url,String web_router) {
    if (TextUtils.isEmpty(app_success_url)) {
        return;
    }
    try {
        Class postcardClazz = Class.forName("com.alibaba.android.arouter.facade.Postcard");
        Constructor postCardConstructor = postcardClazz.getConstructor(String.class, String.class);
//构造实例
        Object postcard = postCardConstructor.newInstance(TextUtils.isEmpty(web_router)?"/web/component":web_router, "web");
//获取方法withString,参数类型
        Method withStringMethod = postcardClazz.getMethod("withString", new Class[]{String.class, String.class});
//invoke 调用
        withStringMethod.invoke(postcard, new Object[]{"url", app_success_url});
//获取方法navigation,加参数类型
        Method navigationMethod = postcardClazz.getMethod("navigation", Context.class);
//调用
        navigationMethod.invoke(postcard, activity);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读