信息发布→ 登录 注册 退出

Android中的深度链接技术实战

发布时间:2026-01-11

点击量:
目录
  • 前言
  • Deep Links
    • 示例
    • 注意事项
  • App Links
    • Intent Filter
    • 配置 assetlinks.json
  • 参考文档

    前言

    日常中,我们经常需要从浏览器中的网页或者从其它APP中直接打开我们的APP,我们就需要使用到深度链接技术。实现方式分别是 Dee pLinks 和 APP Links。

    Deep Links

    deep links是谷歌支持的一种打开app指定页面的方式,常用于从H5页面跳转至app目标页面。其对应指定页面的匹配规则是按照URI来匹配的。常见URI格式如下图:

    示例

    • H5测试页面
    <html>
    <a href="http://demo.deaven.com:2003/test/data?params1=value1&params2=value2" rel="external nofollow" >点击唤起app</a>
    <a href="https://demo.deaven.com:2003/test/data?params1=value1&params2=value2" rel="external nofollow" >点击唤起app</a>
    <a href="abc://demo.deaven.com:2003/test/data?params1=value1&params2=value2" rel="external nofollow" >点击唤起app</a>
    </html>

    如上

    • scheme = http、https、abc。 DeepLink中 scheme 可自定义
    • host = demo.deaven.com
    • port = 2003
    • path = test/data
    • 传递参数(key-value): params1 : value1 params2 : value2
    • Android配置
     <activity android:name=".MainActivity"
               android:exported="true"
               android:launchMode="singleTask">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <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="http" /> 
                    <data android:scheme="https" />
                    <data android:scheme="abc" />
                    <data android:host="demo.deaven.com"/>
                    <data android:port="2003"/>
                    <!--表示匹配 Path 以/test 开头的uri,此项可以不写-->
                    <!-- 注意 "/" 在pathPrefix中是必须的-->
                    <data android:pathPrefix="/test"/>
    
                </intent-filter>
            </activity>

    3.Activity中解析Intents

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Uri uri = getIntent().getData();
        String scheme = uri.getScheme(); // http、https、abc
        String host = uri.getHost(); // demo.deaven.com
        String path = uri.getPath(); // test/data
        String query = uri.getQuery(); // params1=value1&params2=value2
        String value1 = uri.getQueryParameter("params1"); 
        String value2 = uri.getQueryParameter("params2");
    }

    为了更好的管理以及用户体验,app中可以声明一个中间页根据参数统一分发跳转请求。

    注意事项

    • scheme为 htttp/https 开头的uri,部分浏览器和手机ROM 并不能链接至APP,而是在浏览器中打开了对应的链接。所以做Deep Links时建议全部采用自定义Scheme的形式。

    • 在询问是否用APP打开对应的链接时,如果选择了“取消”并且“记住选择”被勾上,那么下次你再次想链接至APP时就不会有任何反应!!!

    • 不同的host不要写在同一个Intent Filter中,最好为每种匹配规则新建一个Intent Filter

    App Links

    Android在Android 6.0 (API level 23) 及以后加入了App Links , 当用户点击对应的URI 时,会直接启动对应的APP,不会再出现类似Deep Links 中是否打开app 的对话框出现。

    Intent Filter

     <activity android:name=".MainActivity"
               android:exported="true"
               android:launchMode="singleTask"
               android:autoVerify="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <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="http" /> 
                    <data android:scheme="https" />
                    <data android:host="demo.deaven.com"/>
                    <data android:port="2003"/>
                    <!--表示匹配 Path 以/test 开头的uri,此项可以不写-->
                    <!-- 注意 "/" 在pathPrefix中是必须的-->
                    <data android:pathPrefix="/test"/>
    
                </intent-filter>
            </activity>
    • Intent Filter和Deep Links 类似 但是 scheme只能使用 htttp 或 https 不支持自定义scheme。

    • android:autoVerify="true" 会让APP自动在所列的host中去验证,如果验证成功,APP将成为匹配URI默认打开方式。

    配置 assetlinks.json

    • 你可以访问https://developers.google.com/digital-asset-links/tools/generator生成assetlinks.json,如下图:

    如不能翻墙,可复制下方代码修改为自己参数,生成 assetlinks.json文件 ,json文件名只能是 assetlinks 不能自定义

    [{
      "relation": ["delegate_permission/common.handle_all_urls"],
      "target" : { "namespace": "android_app", "package_name": "com.deaven.link",
                   "sha256_cert_fingerprints": [""14:6D:E9:83:C5:73:06:50:D8:EE:B9:95:2F:34:FC:64:16:A0:83:42:E6:1D:BE:A8:8A:04:96:B2:3F:CF:44:E5""] }
    }]

    2.部署assetlinks.json

    我们的host为demo.deaven.com,那么我们就需将assetlinks.json放到https://demo.deaven.com/.well-known/assetlinks.json并可以正常访问。你也可以在 https://developers.google.com/digital-asset-links/tools/generator检查服务器上assetlinks.json是否可访问如下图:

    3.Activity中解析Intents 类似 Deep Links

    参考文档

    https://www.jianshu.com/p/1632be1c2451

    在线客服
    服务热线

    服务热线

    4008888355

    微信咨询
    二维码
    返回顶部
    ×二维码

    截屏,微信识别二维码

    打开微信

    微信号已复制,请打开微信添加咨询详情!