Android 中 Scheme 协议的使用详解

1. 什么是 URL Scheme?

简单的说就是 android 中的一种页面内跳转协议,方便 app 页面的内的跳转

2.什么时候使用

服务器下发跳转路径,客户端根据 服务器下发跳转路径跳转相应的页面

H5 页面点击描点,根据描点具体跳转路径 APP 端跳转具体的页面

APP 端收到服务器端下发的 PUSH 通知栏消息,根据消息的点击跳转路径跳转相关页面

APP 根据 URL 跳转到另外一个 APP 指定页面

3.协议格式

zymobi://3g2win:9999/macthDetail?macthId=222&time=10001

复制代码


4.在 app 中如何使用

在 AndroidManifest.xml 中对activity 标签增加intent-filter 设置Schema

<activity android:name=".SecondActivity">

            <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:scheme="zymobi"

                    android:host="3g2win"

                    android:port="9999"

                    android:path="/macthDetail"

                    />

            </intent-filter>

        </activity>

复制代码

注意:

<action android:name="android.intent.action.VIEW"/>

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

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

复制代码

5.如何调用

1.在 html 中调用非常简单

<a href="zymobi://3g2win:9999/macthDetail?macthId=222&time=10001">打开源生应用指定的页面</a>

复制代码

2.在源生应用中调用也很简单

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("zymobi://3g2win:9999/macthDetail?macthId=222&time=10001"));

startActivity(intent);

复制代码

6.在源生界面获取 uri 和各个参数

Intent intent = getIntent();

        Uri data = intent.getData();  //

        String action = intent.getAction();

        String scheme = intent.getScheme();

        Set<String> categories = intent.getCategories();

        Log.e("TAG", "data==========="+data);

        Log.e("TAG", "action==========="+action);

        Log.e("TAG", "categories==========="+categories);

        Log.e("TAG", "DataString==========="+intent.getDataString());

        Log.e("TAG", "==============================");

        Log.e("TAG", "scheme==========="+scheme);

        Log.e("TAG", "id ==========="+data.getQueryParameterNames());

        Log.e("TAG", "host==========="+data.getHost());

        Log.e("TAG", "path==========="+data.getPath());

        Log.e("TAG", "port==========="+data.getPort());

复制代码

输出结果

4-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: data===========zymobi://3g2win:9999/macthDetail?goodsId=10011002&time=1111

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: action===========android.intent.action.VIEW

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: categories===========null

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: DataString===========zymobi://3g2win:9999/macthDetail?goodsId=10011002&time=1111

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: ==============================

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: scheme===========zymobi

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: id ===========[goodsId, time]

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: host===========3g2win

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: path===========/macthDetail

04-11 18:13:56.335 5198-5198/com.phone.myapplication E/TAG: port===========9999

复制代码

具体含义可以对比传入的参数

7. 判断 Schema 是否有效

判断 Schema 是否有效,也可以说判断应用是否安装(在确定要启动的应用已经配置了 scheme)

app 源生判断 Sheme 是否有效

Intent intent = newIntent(Intent.ACTION_VIEW, Uri.parse("zymobi://3g2win:9999/macthDetail?macthId=222&time=10001"));

List<ResolveInfo> activities =packageManager.queryIntentActivities(intent, 0);

boolean isValid = !activities.isEmpty();

Toast.makeText(this,isValid+"",Toast.LENGTH_LONG).show();

最后

如果你觉得此文对你有一丁点帮助,点个赞。或者可以加入我的开发交流群:780179818 相互学习,我们会有专业的技术答疑解惑

如果你觉得这篇文章对你有点用的话,麻烦请给我们的开源项目点点 star:http://github.crmeb.net/u/lsq不胜感激 !

PHP 学习手册:https://doc.crmeb.com

技术交流论坛:https://q.crmeb.com

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容