【大前端】Android Deep Link 技术详解与实践指南

在 Android 应用开发中,我们经常会遇到这样的场景:

  • 从短信、网页、推送或第三方 App 点击链接后,直接打开自己 App 的指定页面

  • H5 页面跳转 App 实现无缝衔接;

  • 营销活动链接带参数打开应用指定内容。

这些功能的核心实现手段,就是 Deep Link(深度链接)


一、什么是 Deep Link?

Deep Link 简单来说就是一种"从外部链接直接唤起 App 内特定页面 "的机制。

当用户点击一个符合规则的 URL(例如 myapp://detail?id=123)时,系统会解析该链接并唤起对应的 App 页面。

类比网页:

  • 普通网页链接跳转是 https://example.com/detail?id=123

  • Deep Link 则是 myapp://detail?id=123,它会跳转到 App 内 detail 页面


Deep Link 在 Android 生态中主要分为三种类型:

类型 说明 示例
Custom URI Scheme 自定义协议,最基础、最早的 Deep Link 方案 myapp://product/detail?id=1
Android App Links Android 6.0+ 推出的官方方案,支持 HTTPS 验证,安全性高 https://www.example.com/product/1
Intent Filters with HTTP/HTTPS 普通的 HTTP 链接匹配,不需要域名验证 http://m.example.com/detail?id=1

本文主要聚焦 Custom URI Scheme + App Links 两种主流实现方式。


1. Manifest 配置

在目标页面的 Activity 中声明一个 Intent Filter:

XML 复制代码
<activity
    android:name=".ui.DetailActivity"
    android:exported="true">
    
    <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="myapp"
            android:host="detail" />
    </intent-filter>
</activity>

2. 链接触发示例

  • 网页中:

    复制代码
    <a href="myapp://detail?id=10086">打开App详情页</a>
  • ADB 模拟触发:

    复制代码
    adb shell am start -a android.intent.action.VIEW -d "myapp://detail?id=10086"

3. 获取参数

DetailActivity 中通过 Intent 获取参数:

Kotlin 复制代码
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val data: Uri? = intent.data
    val id = data?.getQueryParameter("id")
    Log.d("DeepLink", "detail id = $id")
}

相比自定义 scheme,App Links 具有以下优势:

  • 使用标准 HTTPS 链接;

  • 只有验证过的域名才会触发应用打开;

  • 避免了多个 App 注册相同 scheme 冲突的问题。

1. Manifest 配置

XML 复制代码
<activity
    android:name=".ui.DetailActivity"
    android:exported="true">

    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        
        <data android:scheme="https"
              android:host="www.example.com"
              android:pathPrefix="/detail" />
    </intent-filter>
</activity>

2. 域名验证文件

在服务器根目录放置一个文件:

路径: https://www.example.com/.well-known/assetlinks.json
内容:

复制代码
[
  {
    "relation": ["delegate_permission/common.handle_all_urls"],
    "target": {
      "namespace": "android_app",
      "package_name": "com.example.myapp",
      "sha256_cert_fingerprints": [
        "A1:B2:C3:D4:...:ZZ"
      ]
    }
  }
]

SHA256 指纹可以通过以下命令获取:

复制代码
keytool -list -v -keystore myapp.keystore

五、从外部浏览器跳转 App

1️⃣ 若 App 已安装

  • 系统自动匹配 Intent Filter 并打开对应 Activity。

2️⃣ 若 App 未安装

  • 可配置 fallback 链接(例如跳转到应用商店下载页):

    打开应用

在大型项目中,为了统一管理各个页面的跳转,可以封装一个 DeepLink 路由管理器:

Kotlin 复制代码
object DeepLinkRouter {
    fun handleDeepLink(context: Context, uri: Uri) {
        when (uri.host) {
            "detail" -> {
                val id = uri.getQueryParameter("id")
                val intent = Intent(context, DetailActivity::class.java)
                intent.putExtra("id", id)
                context.startActivity(intent)
            }
            "profile" -> {
                val userId = uri.getQueryParameter("userId")
                val intent = Intent(context, ProfileActivity::class.java)
                intent.putExtra("userId", userId)
                context.startActivity(intent)
            }
        }
    }
}

然后在入口 Activity(例如 MainActivity)中统一接管:

Kotlin 复制代码
override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    intent?.data?.let { DeepLinkRouter.handleDeepLink(this, it) }
}

七、常见问题与排查技巧

问题 原因 解决方案
点击链接无反应 没有匹配到 Intent Filter 检查 scheme、host、path 是否完全一致
链接被浏览器拦截 未声明 BROWSABLE 添加 <category android:name="android.intent.category.BROWSABLE"/>
App Links 未验证成功 assetlinks.json 配置错误 adb shell am verify-app-links 检查验证状态
多个应用注册相同 scheme 冲突 优先使用 App Links(HTTPS)避免冲突

八、总结

对比项 Custom Scheme App Links
安全性 弱,易被抢注 高,需域名验证
使用门槛 简单,无需服务器 需配置 assetlinks.json
链接样式 myapp:// https://
推荐场景 内部渠道唤起、测试 生产环境、H5落地页、广告跳转

九、结语

Deep Link 是连接 Web 与 App 的桥梁,是移动增长和产品体验的重要一环。

在项目中合理使用 Deep Link,可以极大提升用户转化率与跳转体验。

相关推荐
阿部多瑞 ABU4 天前
# AI高精度提示词生成项目——3D-VR 课件—— 最终仓库级 AI 提示词:生成《EduVR Studio》—— 专业级 3D-VR 课件创作平台
gitee·开源·github·aigc·ai编程·1024程序员节
胖咕噜的稞达鸭4 天前
封装map和set(红黑树作为底层结构如何实现map和set插入遍历)
c语言·数据结构·c++·算法·gitee·哈希算法
期待のcode5 天前
gitee与github远程仓库
gitee·github
summer夏1236 天前
gitee简易的命令入门教程
gitee
AlexMercer10128 天前
Ubuntu从零开始配置Git
c++·git·ubuntu·gitee
像风一样自由202010 天前
代码仓库码云(gitee)配置环境记录
gitee
sulikey10 天前
从零配置一个规范的 Python Git 仓库(适用于 Gitee / GitHub)
git·python·pycharm·gitee·github
全栈小511 天前
【代码管理】在本地使用github和gitee之后,可能存在冲突,导致再次提交代码时提示Couldn‘t connect to server
gitee·github·代码管理工具
春生野草12 天前
Gituee
git·gitee
J2虾虾12 天前
WebStorm的项目绑定Git并上传到gitee
git·gitee·webstorm