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

Android 13中通知DeepLink跳转Composable页面时打开浏览器问题

解决Android 13中DeepLink跳转打开浏览器而非应用的问题

以下是关键排查点和修复方案:

1. 检查Manifest的Intent Filter配置

确保宿主Compose的Activity(通常是MainActivity)的intent-filter配置正确:

  • 必须包含ACTION_VIEW action、CATEGORY_DEFAULT和CATEGORY_BROWSABLE category
  • scheme、host、path(或pathPattern)要和你的DeepLink完全匹配
  • Android 12+可添加android:autoVerify="true"帮助系统优先识别你的应用(需配合assetlinks.json,内部跳转可先跳过)

示例配置:

<activity
    android:name=".MainActivity"
    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="yourapp"
            android:host="example.com"
            android:path="/detail" />
    </intent-filter>
</activity>

2. 修正PendingIntent的创建逻辑

Android 12及以上版本必须指定FLAG_IMMUTABLE或FLAG_MUTABLE,同时显式设置包名确保跳转当前应用:

示例代码:

val deepLinkUri = Uri.parse("yourapp://example.com/detail")
val intent = Intent(Intent.ACTION_VIEW, deepLinkUri).apply {
    setPackage(context.packageName) // 强制指定当前应用包名
    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
}

val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
} else {
    PendingIntent.getActivity(context, 0, intent, 0)
}

3. 确认Compose Navigation的DeepLink配置

确保在NavHost的路由中正确关联DeepLink,并在Activity中传递Intent数据:

路由配置示例:

NavHost(navController = navController, startDestination = "home") {
    composable(
        route = "detail",
        deepLinks = listOf(
            navDeepLink {
                uriPattern = "yourapp://example.com/detail"
            }
        )
    ) {
        DetailScreen()
    }
}

Activity中处理DeepLink:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()
            LaunchedEffect(intent?.data) {
                intent?.data?.let { navController.navigate(it) }
            }
            NavHost(navController = navController, startDestination = "home") {
                // 路由配置
            }
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        intent?.data?.let {
            findNavController(R.id.nav_host_container)?.navigate(it)
        }
    }
}

4. 修复adb测试命令

测试时若浏览器被打开,可指定包名强制启动你的应用:

adb shell am start -W -a android.intent.action.VIEW -d "yourapp://example.com/detail" com.your.package.name

也可手动在系统设置中,将该DeepLink的默认应用切换为你的应用。

额外注意点

  • 避免使用http/https作为scheme(除非配置了assetlinks.json验证),否则系统优先选择浏览器
  • Android 13中应用后台被杀死时,FLAG_ACTIVITY_NEW_TASK是启动应用的必要参数

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 04:32:15