Android路由跳转 之模块化 SDK 后台控制

为什么会用到路由跳转,一般我们在项目中基本使用这种方式

Intent intent = new Intent(MainActivity.this, LoanRequestActivity.class);
startActivity(intent);

也可以使用隐式的方式来跳转,不过一般用的少,多数情况是调用第三方Sdk,系统的相机 相册这种三方的,显示的跳转基本就可以实现,但是对于跳转的来源很多的情况就不适合了
,例如推送Web页面,以及其他应用,并且很多地方的跳转需要设置成服务器配置,于是就用到了
路由跳转,以url的字段标志来识别一个activity来统一进行跳转.

调用方式:

应用内模块间跳转:
RouteDispatch.dispatch(MainActivity.this,"/loan/apply");
其他跳转来源:这里采用一个中间Activity来跳转

public class RouteDispatchActivity extends AppCompatActivity {     
            protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                RouteDispatch.dispatch(this);
                this.finish();
            }

实现步骤:

1.构建路由对象

public class Route {
     //自定义的Scheme 和 Host
    public static final String JS_SCHEMA = "js";
    public static final String PUSH_SCHEMA = "push";
    public static final String APP_SCHEMA = "renwohua";
    public static final String HTTP_SCHEMA = "http";
    public static final String HTTPS_SCHEMA = "https";
    public static final String HOST = "com.renwohua.conch";
    private Class<?> activityClass;
    private Uri mUri;

     //将 url  和  Activity 组成Route对象
    public Route(String uri, Class<?> activityClass) {
        this.mUri = Uri.parse(uri);
        this.activityClass = activityClass;
    }

    public boolean isJS() {
        if(this.mUri == null) {
            throw new NullPointerException("uri == null");
        } else {
            return "js".equals(this.mUri.getScheme()) && "com.renwohua.conch".equals(this.mUri.getHost());
        }
    }

    public static boolean isJS(String url) {
        if(url == null) {
            throw new NullPointerException("url == null");
        } else {
            Uri uri = Uri.parse(url);
            return "js".equals(uri.getScheme()) && "com.renwohua.conch".equals(uri.getHost());
        }
    }

    public boolean isPush() {
        if(this.mUri == null) {
            throw new NullPointerException("uri == null");
        } else {
            return "push".equals(this.mUri.getScheme()) && "com.renwohua.conch".equals(this.mUri.getHost());
        }
    }

    public boolean isAPP() {
        if(this.mUri == null) {
            throw new NullPointerException("uri == null");
        } else {
            return "renwohua".equals(this.mUri.getScheme()) && "com.renwohua.conch".equals(this.mUri.getHost());
        }
    }

    public static boolean isAPP(String url) {
        if(url == null) {
            throw new NullPointerException("url == null");
        } else {
            Uri uri = Uri.parse(url);
            return "renwohua".equals(uri.getScheme()) && "com.renwohua.conch".equals(uri.getHost());
        }
    }

    public boolean isHTTP() {
        if(this.mUri == null) {
            throw new NullPointerException("uri == null");
        } else {
            return "http".equals(this.mUri.getScheme()) || "https".equals(this.mUri.getScheme());
        }
    }

    public static boolean isHTTP(String url) {
        if(url == null) {
            throw new NullPointerException("url == null");
        } else {
            Uri uri = Uri.parse(url);
            return "http".equals(uri.getScheme()) || "https".equals(uri.getScheme());
        }
    }

    public Class<?> getActivityClass() {
        return this.activityClass;
    }
}

2.构建路由对象集合

public final class Routes {
    private final Map<String, Class<? extends Activity>> map = new HashMap();
    private static String schema = "renwohua";//默认的schema
    private static String host = "com.renwohua.conch";//默认的host

    public Routes() {
    }
    //添加一个路由集合
    public Routes add(Routes routes) {
        if(routes != null) {
            this.map.putAll(routes.map);
        }

        return this;
    }
    //添加单个路由
    public Routes add(String activityKey, Class<? extends Activity> activityClass) {
        if(activityKey == null) {
            throw new IllegalArgumentException("The activityKey can not be null");
        } else {
            activityKey = activityKey.trim();
            if("".equals(activityKey)) {
                throw new IllegalArgumentException("The activityKey can not be blank");
            } else if("/".equals(activityKey)) {
                throw new IllegalArgumentException("The activityKey can not be /");
            } else if(activityClass == null) {
                throw new IllegalArgumentException("The activityClass can not be null");
            } else {
                if(!activityKey.startsWith("/")) {
                    activityKey = "/" + activityKey;
                }

                if(this.map.containsKey(activityKey)) {
                    throw new IllegalArgumentException("The activityKey already exists: " + activityKey);
                } else {
                    this.map.put(activityKey, activityClass);
                    return this;
                }
            }
        }
    }

    public Set<Entry<String, Class<? extends Activity>>> getEntrySet() {
        return this.map.entrySet();
    }
    //获取单个路由对象
    public Route getRoute(String path) {
        Class activityClass = (Class)this.map.get(path);
        return activityClass == null?null:new Route(this.getView(path), activityClass);
    }
    //组装url = schema://host/path
    public String getView(String path) {
        return schema + "://" + host + path;
    }
    //外部设置schema
    static void setSchema(String schema) {
        if(schema == null) {
            throw new IllegalArgumentException("The schema can not be null");
        } else {
            schema = schema.trim();
            if("".equals(schema)) {
                throw new IllegalArgumentException("The schema can not be blank");
            } else {
                schema = schema;
            }
        }
    }
    //外部设置host
    static void setHost(String host) {
        if(host == null) {
            throw new IllegalArgumentException("The host can not be null");
        } else {
            host = host.trim();
            if("".equals(host)) {
                throw new IllegalArgumentException("The host can not be blank");
            } else {
                host = host;
            }
        }
    }
}

3.Application中配置定义的路由

   @Override
    public void configRoute(Routes me) {
        me.add("/browser", BrowserActivity.class);
        me.add("/credit/apply", QuotaApplyActivity.class);
        me.add("/credit/work", StyleActivity.class);
        。。。

4.路由分发器


public class RouteDispatch {
    private static final String TAG = RouteDispatch.class.getSimpleName();

    private RouteDispatch() {
        throw new AssertionError("No instances.");
    }
     //根据某个Activity获取Application中定义的路由配置 
    private static Routes getRoutes(Activity activity) {
        if(activity == null) {
            throw new NullPointerException("activity == null");
        } else {
            BaseApplication application = (BaseApplication)activity.getApplication();
            return BaseApplication.getRoutes();
        }
    }
      
    static void dispatch(Activity activity) {
        if(activity == null) {
            throw new NullPointerException("activity == null");
        } else {
            dispatch(activity, activity.getIntent());
        }
    }
    
  //根据不同的来源url组装不同的path路径,去启动与之对应的Activity
    public static void dispatch(Activity activity, String url) {
        if(activity == null) {
            throw new NullPointerException("activity == null");
        } else if(url == null) {
            throw new IllegalArgumentException("The url can not be null");
        } else {
            url = url.trim();
            if("".equals(url)) {
                throw new IllegalArgumentException("The url can not be blank");
            } else {
                Log4a.d(url);
                Uri uri = Uri.parse(url);
                String path = "";
                String params = "";
                if(StringKit.isEmpty(uri.getScheme())) {
                    path = uri.getPath();
                } else if(!uri.getScheme().equals("http") && !uri.getScheme().equals("https")) {//来自app
                    if(!uri.getScheme().equals("renwohua")) {
                        Log4a.e(TAG, new Object[]{"No registered route to handle uri:" + uri.toString()});
                        return;
                    }

                    path = uri.getPath();//
                    if(uri.getQueryParameterNames().size() > 0) {
                        params = "?" + uri.getQuery();
                    }
                } else {//来自网络  
                    path = "/browser";
                    params = "?url=" + Uri.encode(url);
                }

                dispatch(activity, path, params);
            }
        }
    }
    //根据path  Params 组建url  添加到Intent中
    public static void dispatch(Activity activity, String path, String params) {
        if(activity == null) {
            throw new NullPointerException("activity == null");
        } else if(path == null) {
            throw new IllegalArgumentException("The path can not be null");
        } else {
            path = path.trim();
            if("".equals(path)) {
                throw new IllegalArgumentException("The path can not be blank");
            } else {
                Log4a.d(path);
                Routes routes = getRoutes(activity);
                if(StringKit.isEmpty(params)) {
                    params = "";
                } else {
                    params = params.startsWith("?")?params:"?" + params;
                }

                String view = routes.getView(path) + params;
                Intent intent = new Intent("android.intent.action.VIEW");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse(view));
                dispatch(activity, intent);
            }
        }
    }

    //跳转 并将Params放置到Intent中
    public static void dispatch(Activity activity, Intent sourceIntent) {
        if(activity == null) {
            throw new NullPointerException("activity == null");
        } else if(sourceIntent == null) {
            throw new NullPointerException("sourceIntent == null");
        } else {
            Uri uri = sourceIntent.getData();
            if(uri == null) {
                Log4a.e(TAG, new Object[]{"No Uri in given activity\'s intent."});
            } else {
                Routes routes = getRoutes(activity);
                Route entry = routes.getRoute(uri.getPath());
                if(entry == null) {
                    Log4a.e(TAG, new Object[]{"No registered route to handle uri:" + uri.toString()});
                } else {
                    Bundle parameters = null;
                    if(sourceIntent.getExtras() != null) {
                        parameters = new Bundle(sourceIntent.getExtras());
                    } else {
                        parameters = new Bundle();
                    }

                    Iterator c = uri.getQueryParameterNames().iterator();

                    while(c.hasNext()) {
                        String newIntent = (String)c.next();
                        parameters.putString(newIntent, uri.getQueryParameter(newIntent));
                    }

                    Class c1 = entry.getActivityClass();
                    Intent newIntent1 = new Intent(activity, c1);
                    if(newIntent1.getAction() == null) {
                        newIntent1.setAction(sourceIntent.getAction());
                    }

                    if(newIntent1.getData() == null) {
                        newIntent1.setData(sourceIntent.getData());
                    }

                    newIntent1.putExtras(parameters);
                    if(activity.getCallingActivity() != null) {
                        newIntent1.setFlags(33554432);
                    }

                    if(!AppKit.isAvailableIntent(newIntent1)) {
                        Log4a.e(TAG, new Object[]{"No registered route to handle uri:" + uri.toString()});
                    } else {
                        activity.startActivity(newIntent1);
                    }
                }
            }
        }
    }
}

5.具体使用:

第三方调用:

1.建立一个路由分发器,在Manifest中注册路由分发器的接收规则

 <!-- 路由分发器 -->
        <activity
            android:name="com.renwohua.frame.route.RouteDispatchActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="com.renwohua.conch"
                    android:scheme="renwohua"/>
            </intent-filter>
        </activity>
        <!-- 路由分发器 -->

2.路由分发器
这里作为一个路由分发的中转站,接收所有intent-filter中匹配的第三方 Web的请求,具体操作在上面的RouteDispatch中

public class RouteDispatchActivity extends AppCompatActivity {
    public RouteDispatchActivity() {
    }

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RouteDispatch.dispatch(this);
        this.finish();
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353

推荐阅读更多精彩内容