Android Deep links

Deep links是可以让用户直接进入应用的特定部分的URI。

  • 创建,在Manifest中的activity声明中添加intent过滤器
ini 复制代码
<activity
    android:name=".MyMapActivity"
    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="geo" />
    </intent-filter>
</activity>
  • 当存在多个能够处理相同scheme的应用时,app会打开一个对话框选择。

  • 被调用方使用:获取intent.data,拆分解析,组件封装,通过规则+Arouter跳转目标业务。

官方文档:developer.android.com/training/ap...

APP links:scheme为https,并且有签名等机制

  1. 准备域名和资产关联文件

    • 确保你拥有一个 HTTPS 域名,比如 www.example.com

    • 在该域名的服务器根目录下创建一个 .well-known/assetlinks.json 文件,内容类似:

      json 复制代码
      [
        {
          "relation": ["delegate_permission/common.handle_all_urls"],
          "target": {
            "namespace": "android_app",
            "package_name": "com.yourcompany.yourapp",
            "sha256_cert_fingerprints": [
              "AA:BB:CC:...:YY:ZZ"    // 你用来签名 APK 的证书指纹(SHA‑256)
            ]
          }
        }
      ]
    • 部署后,在浏览器访问 https://www.example.com/.well-known/assetlinks.json 能拿到该 JSON。

  2. 在 AndroidManifest.xml 中声明 Intent Filter

    在你希望处理 App Link 的 Activity 节点里添加:

    xml 复制代码
    <activity android:name=".YourDeepLinkActivity">
      <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" />
    
        <!-- 匹配你的 HTTPS 链接 -->
        <data
          android:scheme="https"
          android:host="www.example.com"
          android:pathPrefix="/applink" />
      </intent-filter>
    </activity>
    • android:autoVerify="true":安装时系统自动向服务器验证 assetlinks.json
  3. 签名与构建

    • 用与你在 assetlinks.json 中声明的同一套发布签名(release keystore)对 APK 进行签名。
    • 安装到设备或模拟器上。
  4. 系统验证

    • 安装 APK 后,Android 框架会向 https://www.example.com/.well-known/assetlinks.json 发起请求,检查 package_name 与证书指纹是否匹配。
    • 验证成功后,这个 App 就被注册为该域名的"所有者",当用户点击符合规则的链接时会优先唤起你的 App。
  5. 测试你的 App Link

    • 命令行测试

      sql 复制代码
      adb shell am start \
        -a android.intent.action.VIEW \
        -c android.intent.category.BROWSABLE \
        -d "https://www.example.com/applink/page/123"
    • 检查验证状态

      arduino 复制代码
      adb shell pm get-app-links com.yourcompany.yourapp

      或(Android 11+)

      css 复制代码
      adb shell pm verify-app-links --re-verify com.yourcompany.yourapp

      查看输出中是否有 verified

  6. 在代码中处理跳转

    • YourDeepLinkActivityonCreate()/onNewIntent() 中,用 intent.data 拿到 Uri,再解析出 pathSegmentsqueryParameter

      kotlin 复制代码
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        intent.data?.let { uri ->
          val segments = uri.pathSegments   // e.g. ["applink","page","123"]
          val pageId = segments.getOrNull(2)
          // 根据 pageId 展示对应页面
        }
      }
      override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        intent.data?.let { handleDeepLink(it) }
      }
  7. 优雅回退

    • 如果 App 未安装,点击 HTTPS 链接会在浏览器打开对应网页。
    • 你也可以在网页中检测 UA 或 JS 判断,提示用户下载或跳转到应用市场。

总结

  1. 服务器端 :部署 assetlinks.json,声明包名+证书指纹。
  2. 客户端 :AndroidManifest 中配置 <intent-filter android:autoVerify="true">;用 release 签名构建并安装。
  3. 验证与测试:系统自动验证并响应点击,或用 ADB 命令手动测试。
  4. 业务处理 :在 Activity 中通过 intent.data 获取并解析 URL 参数。
相关推荐
LaughingZhu6 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫6 小时前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux6 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水7 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger7 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)8 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态8 小时前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态8 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart8 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
放下华子我只抽RuiKe58 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架