You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过单链接实现安卓应用已安装则打开否则跳转谷歌商店?

安卓单链接跳转应用/谷歌商店实现方案

一、正确的链接格式

你需要用应用链接(App Links)或带 fallback 的自定义 Scheme 链接来实现单链接需求:

  • 带 fallback 的通用链接(无需配置域名,直接兼容已安装/未安装):
    intent://<host>/<path>#Intent;scheme=<你的自定义Scheme>;package=<你的应用包名>;S.browser_fallback_url=https://play.google.com/store/apps/details?id=<你的应用包名>;end
    
    示例:intent://app/home#Intent;scheme=myapp;package=com.example.myapp;S.browser_fallback_url=https://play.google.com/store/apps/details?id=com.example.myapp;end
  • 标准 App Links 格式(需要配置域名和assetlinks.json):
    https://<你的公网域名>/<深层链接路径>
    
    示例:https://myapp.com/deep/home

二、Compose Navigation 具体实现步骤

1. 配置 AndroidManifest.xml

在启动 Activity(一般是 MainActivity)中添加 Intent Filter,让系统识别你的链接:

<activity
    android:name=".MainActivity"
    android:exported="true">
    <!-- 处理自定义 Scheme 链接 -->
    <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" <!-- 替换成你的自定义 Scheme -->
            android:host="app" <!-- 自定义主机名 -->
            android:pathPrefix="/home" /> <!-- 匹配的路径前缀 -->
    </intent-filter>
    <!-- 处理 App Links(需配置 assetlinks.json) -->
    <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="myapp.com" <!-- 替换成你的公网域名 -->
            android:pathPrefix="/deep/home" /> <!-- 匹配的路径前缀 -->
    </intent-filter>
</activity>

2. 在 Compose Navigation 中绑定深层链接

在你的 NavHost 里配置对应的 deepLink,让导航组件处理跳转后的页面:

NavHost(navController = navController, startDestination = "home") {
    composable(
        route = "home",
        deepLinks = listOf(
            // 匹配自定义 Scheme 链接
            navDeepLink { uriPattern = "myapp://app/home" },
            // 匹配 App Links 链接
            navDeepLink { uriPattern = "https://myapp.com/deep/home" }
        )
    ) {
        HomeScreen() // 跳转后显示的页面
    }
}

三、未安装应用的跳转逻辑

  • 使用带 fallback 的自定义 Scheme 链接:浏览器会自动检测应用状态,已安装则直接唤起应用;未安装则跳转到 fallback 指定的谷歌商店链接。
  • 使用App Links:当应用未安装时,点击链接会打开你配置的域名页面,你需要在该页面添加跳转谷歌商店的逻辑(比如一个引导按钮,或者自动跳转的 JS 脚本)。
  • 是否需要?:如果用标准 App Links(https 域名的链接),必须添加这个文件。它的作用是让安卓系统验证你的应用和域名的关联关系,确保点击链接时直接唤起应用,而不弹出选择器。
  • 配置方法:
    1. 生成assetlinks.json文件,内容模板如下(替换成你的包名和签名指纹):
    [{
      "relation": ["delegate_permission/common.handle_all_urls"],
      "target": {
        "namespace": "android_app",
        "package_name": "com.example.myapp",
        "sha256_cert_fingerprints": ["你的应用签名SHA256指纹"]
      }
    }]
    
    1. 将文件上传到你域名的.well-known目录下,确保可以通过https://你的域名/.well-known/assetlinks.json访问到该文件。

内容的提问来源于stack exchange,提问作者tasjapr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 15:07:46