Code Monkey home page Code Monkey logo

navrouter's Introduction

NavRouter

简介

支持给activity绑定url,从而利用url唤起activity,并支持浏览器跳应用页面。可结合Android Studio 2.3 自带的App Links Assistanturlactivity的映射关系进行统一管理,使用详情可参见我的上一篇文章Android Studio 2.3 特性:生成Android App Links

使用

1.集成

由于暂时未将库上传至jCenter,目前需要手动引入navrouter模块进行调用(后期补上)

已将库上传至jcenter,集成仅需要调用下列代码: compile 'com.leozheng:navrouter:1.0'

2.使用

2.1 配置映射关系

AndroidManifest中配置activityurl的映射关系,其中data标签配置的scheme、host、path即为给定url的相关信息。 AndroidManifest

可利用Android Studio 2.3中的App Links Assistant进行统一管理,并直接输入url测试是否关联成功。

2.2 调用跳转

直接调用下来方法,传入context和对应的url即可跳转指定的页面

NavRouter.from(context).toUri("http://blog-debug.leozheng.com/test");

2.3 支持获取参数

activityoncreate可以通过intent拿到原始的uri,而uri包含了所有参数的信息,使用uri.getQueryParameter(key)就能得到对应的值。

Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData(); //传递原始信息的Uri

由于取出的为String类型,为了方便使用,通过ParamUtils工具类能简单获取给定类型的值。例如: int year = ParamUtils.getIntExtra(appLinkData, "year", 0);

同样,使用者也可以根据个人的需求,封装其他的方法(如将jsonString转换为对应的实体,这里也就不展开了)。

2.4 支持CallBack

使用者可以利用两种方法实现CallBack

1.在Application中实现NavRouterCallBackFactory,即可实现全局统一控制

public class RouterApplication extends Application implements NavRouterCallBackFactory {

    @Override
    public NavRouterCallBack provideNavRouterCallBack() {
        return new NavRouterCallBack() {
            @Override
            public boolean beforeOpen() {
                Log.d("NavRouter", "beforeOpen from Application");

                return true; //此处可添加跳转前置判断
            }

            @Override
            public void afterOpen() {
                Log.d("NavRouter", "afterOpen from Application");
            }

            @Override
            public void onError(Intent intent) {
                Toast.makeText(getApplicationContext(), "There is no activity matched for " + intent, Toast.LENGTH_SHORT).show();
            }
        };
    }
}

2.在Activity中使用,可根据某一特定activity进行专门配置

                 * Show the custom NavRouterCallBack.Compared with default callback {@link RouterApplication}
                 */
                NavRouter.from(MainActivity.this).toUri(((TextView) v).getText().toString(), -1, new NavRouterCallBack() {
                    @Override
                    public boolean beforeOpen() {
                        Log.d("NavRouter", "beforeOpen from " + MainActivity.class.getSimpleName());
                        Toast.makeText(MainActivity.this, MainActivity.class.getSimpleName() + " was reject in Activity", Toast.LENGTH_SHORT).show();
                        return false;
                    }
                    @Override
                    public void afterOpen() {
                        Log.d("NavRouter", "afterOpen from " + MainActivity.class.getSimpleName());
                    }
                    @Override
                    public void onError(Intent intent) {
                        Log.d("NavRouter", "beforeOpen from " + MainActivity.class.getSimpleName());
                    }
                });

2.5 支持传Bundle

调用NavRouter.from(context).putExtra(key,bundle).toUri(Uri) 可传递Bundle给对应的activity,并用getIntent().getBundleExtra(key)取得对应的Bundle。

2.6 支持外部浏览器及WebView唤起

WebView中唤起可参考Demo中的NavWebViewClient进行自定义定制。而从浏览器唤起时,可通过控制需要唤起的activitylaunchMode修改页面的启动模式

开源协议

Apache License 2.0

联系作者

navrouter's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.