【大前端】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,可以极大提升用户转化率与跳转体验。

相关推荐
小萌新上大分4 小时前
Typora 配置 PicGo 使用 Gitee 图床实现图片自动上传(Mac 详细教程)
macos·gitee·typora图床·gitee图床·picgo配置gitee·typora配置图床·typora的图床gitee
南部余额20 小时前
gitee设置不公开邮箱地址,推送报错解决方案
gitee
爱吃烤鸡翅的酸菜鱼2 天前
深度掌握 Git 分支体系:从基础操作到高级策略与实践案例
分布式·git·后端·gitee·github
小黄酥3 天前
Sourcetree克隆/获取gitee工程,Git获取SSH密钥
git·gitee·github
海绵宝龙6 天前
将若依(RuoYi)项目创建为私有Gitee仓库的完整步骤
前端·gitee
雾削木9 天前
通过 “移动文件 + 创建符号链接”,让 Android Studio
gitee
wei84406787211 天前
Android实现RecyclerView粘性头部效果,模拟微信账单列表的月份标题平移
android·java·微信·gitee
倔强的石头10615 天前
解决Markdown笔记图片失效问题:Gitee+PicGo图床搭建全攻略
笔记·gitee·picgo·obsidian
老黄编程17 天前
gitee.com 有raw.githubusercontent.com一样的机制吗?
gitee