美文网首页
Android URL Scheme

Android URL Scheme

作者: 皓皓amous | 来源:发表于2019-05-10 10:47 被阅读0次

    我们在使用微信的过程中,经常可以通过别人分享的链接,直接跳转到一些APP内部。比如京东、网易云音乐,我们通过在这两个APP内部分享链接到微信,微信中点击链接进入微信浏览器打开分享的页面,可以在页面中看到直接打开的按钮,点击直接打开就进入了该APP的对应的页面。实际上就是从微信浏览器中的一个页面跳转到APP指定的页面。这一种效果就是通过URL Scheme来实现的。

    image image image image

    URL Scheme

    URL Scheme是一种页面内跳转协议,通过定义自己的URL Scheme协议,可以
    从一个APP中打开另外一个APP指定的页面,也可以从H5页面中跳转到APP指定的页面(实际上就是从一个浏览器中的一个页面跳转到APP指定页面)。

    URL Scheme协议格式:

    一个完整的完整的URL Scheme协议格式由scheme、host、port、path和query组成,其结构如下所示:

    <scheme>://<host>:<port>/<path>?<query>
    

    scheme可以是常见的协议名 (http、file等)也可以是自定义的协议名(自定义一个字符串即可),一般打开一个APP,大多使用自定义的协议名。

    如下就是一个自定义的URL
    caishilive://caishi:8080/loadtooldetail?tool_id=100
    caishilive:即Scheme 该Scheme协议名称
    caishi:即Host,代表Scheme作用于哪个地址域
    8080:即port,代表端口号
    loadtooldetail:即path,代表打开的页面
    tool_id:即query,代表传递的参数

    URL Scheme使用
    URL Scheme的使用要先在AndroidManifest.xml中配置能接受Scheme方式启动的activity;

      <activity
                android:name=".ui.tool.LoadToolDetailActivity"
                android:screenOrientation="portrait">
                <!--要想在别的App上能成功调起App,必须添加intent过滤器-->
                <intent-filter>
                    <!--协议部分,随便设置-->
                    <data
                        android:host="caishi"
                        android:path="/loadtooldetail"
                        android:port="8080"
                        android:scheme="caishilive"/>
                    <!--下面这几行也必须得设置-->
                    <category android:name="android.intent.category.DEFAULT"/>
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.BROWSABLE"/>
                </intent-filter>
            </activity>
    

    获取Scheme跳转的参数

     Intent intent = getIntent();
            String scheme = intent.getScheme();
            String dataString = intent.getDataString();
            Uri uri = intent.getData();
            if (uri != null) {
                //完整的url信息
                String url = uri.toString();
                //scheme部分
                String schemes = uri.getScheme();
                //host部分
                String host = uri.getHost();
                //port部分
                int port = uri.getPort();
                //访问路径
                String path = uri.getPath();
                //编码路径
                String path1 = uri.getEncodedPath();
                //query部分
                String queryString = uri.getQuery();
                //获取参数值
                String systemInfo = uri.getQueryParameter("tool_id");
        } 
    

    调用方式:

    (1)网页调用

    <a href="caishilive://caishi:8080/loadtooldetail?tool_id=100">打开APP工具详情页</a>
    

    (2)APP上调用

         Intent action = new Intent(Intent.ACTION_VIEW);
         StringBuilder builder = new StringBuilder();
         builder.append("caishilive://caishi:8080/loadtooldetail?tool_id=100");
         action.setData(Uri.parse(builder.toString()));
         startActivity(action);
    

    当然跳转前要判断一下该URL Scheme是否有效

    private boolean schemeValid() {
            PackageManager manager = mContext.getPackageManager();
            Intent action = new Intent(Intent.ACTION_VIEW);
            action.setData(Uri.parse("caishilive://caishi:8080/loadtooldetail?tool_id=100"));
            List list = manager.queryIntentActivities(action, PackageManager.GET_RESOLVED_FILTER);
            return list != null && list.size() > 0;
        }
    

    以上就是URL Scheme的使用

    相关文章

      网友评论

          本文标题:Android URL Scheme

          本文链接:https://www.haomeiwen.com/subject/hnpooqtx.html