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

Android开发:如何让应用仅响应无查询参数的Deep Link?

实现无查询参数Deep Link打开应用,带参数链接跳转浏览器

这个需求完全可以实现,通过优化Manifest配置和修正代码逻辑即可解决你的问题:

1. 保留现有Intent Filter配置

你的Manifest配置已经能匹配根路径的无参数链接,Android的Intent Filter会忽略查询参数,所以带参数的链接依然会触发应用,我们后续通过代码过滤即可,无需修改现有配置:

<intent-filter
    android:autoVerify="true"
    tools:targetApi="m">
    <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" />
    <data android:host="www.example.com" />
    <data android:path="/" />
</intent-filter>

2. 修正代码逻辑,避免循环并正确跳转浏览器

你之前的代码存在两个关键错误:启动浏览器时误用了原Intent,且未关闭当前Activity导致循环。修正后的代码如下:

fun handleIntent(intent: Intent, activity: Activity) {
    val uri = intent.data ?: return
    // 检查是否存在任何查询参数,若需过滤特定参数可改为检查"section"
    if (uri.query != null) {
        val openInBrowserIntent = Intent(Intent.ACTION_VIEW, uri).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            // 可选:指定浏览器包名(如Chrome的com.android.chrome),彻底避免跳转回应用
            // setPackage("com.android.chrome")
        }
        activity.startActivity(openInBrowserIntent)
        // 关闭当前Activity,消除循环触发的可能
        activity.finish()
    }
    // 无查询参数时,正常执行应用内逻辑
}

核心要点

  • 用uri.query != null可过滤所有带参数的链接,若仅需拦截特定参数(如section),可保留!uri.getQueryParameter("section").isNullOrBlank()判断
  • 组合使用FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK,确保浏览器启动时与应用任务栈完全分离
  • 调用activity.finish()关闭当前页面,彻底避免应用被重复触发的循环问题
  • 可选指定浏览器包名,进一步保证链接不会跳转回你的应用

配置完成后,用户点击https://www.example.com/会直接打开应用,点击带查询参数的链接则会启动浏览器,完全符合你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 23:12:49