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

相关推荐
C澒11 小时前
Gitee 分支管理规范
gitee·团队开发
梁下轻语的秋缘15 小时前
从零到一:本地项目上传Gitee完整指南(新手避坑版)
gitee
TheNextByte115 小时前
如何在Android上恢复已删除的文件
android·gitee
玄同7653 天前
让 Trae IDE 智能体 “读懂”文档 Excel+PDF+DOCX :mcp-documents-reader 工具使用指南
人工智能·git·语言模型·gitee·github·ai编程·mcp
2301_805962934 天前
树莓派的一些问题记录-1:usbboot仓库
python·gitee
COSMOS_*5 天前
2025最新版 Android Studio安装及组件配置(SDK、JDK、Gradle)
android·ide·jdk·gitee·android studio
Mo_YuO.o5 天前
工作区 暂存区 版本库
git·gitee·github
深念Y5 天前
本地Git仓库推送到Gitee教程
git·gitee·github·仓库·项目·源代码管理·初始化
TheNextByte16 天前
如何将手机中的视频传输到电脑上?
智能手机·gitee·电脑
Micro麦可乐6 天前
最新Spring Security实战教程(十五)快速集成 GitHub 与 Gitee 的社交登录
java·spring boot·spring·gitee·github·spring security·社交登陆