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

Android端默认打开URL时排除指定子域名的技术咨询

Android端默认打开URL时排除指定子域名的技术咨询

嘿,我之前也帮朋友处理过类似的需求,Android 系统本身的 Intent Filter 确实没有直接提供「排除特定子域名」的配置项,但咱们可以通过几个实用的 workaround 来实现这个目标,给你梳理下可行的方案:

  • 拆分 Intent Filter,精准配置允许的子域名
    如果你们需要排除的子域名数量不多,而允许的子域名是可控的,那可以把原来的通配符 *.example.com 拆分成多个具体的 Intent Filter,只给需要允许的子域名配置规则,完全不配置要排除的子域名。比如你要排除 test.example.com,就给 a.example.com、b.example.com 这些需要跳转 App 的子域名分别加 Intent Filter:

    <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="a.example.com" />
    </intent-filter>
    <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="b.example.com" />
    </intent-filter>
    

    这种方法的好处是完全遵循系统的匹配逻辑,不会有额外的跳转拦截步骤,但如果允许的子域名数量特别多的话,配置起来会有点繁琐。另外别忘了每个子域名都要配置对应的 assetlinks.json 来通过 App Links 的验证哦。

  • 在 App 内做拦截跳转处理
    如果你们的子域名数量太多,没法一个个配置 Intent Filter,那可以保留原来的通配符 Intent Filter,让所有符合规则的 URL 都先跳转到 App 里,然后在接收 Intent 的 Activity 里做判断拦截:

    1. 在 Activity 的 onCreate 或者 onNewIntent 方法中,获取跳转过来的 URL 主机地址;
    2. 检查这个主机地址是不是需要排除的子域名(比如 exclude.example.com);
    3. 如果是排除的域名,就调用系统浏览器打开这个 URL,然后关闭当前的 Activity。

    给你个简单的代码示例:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        intent?.data?.host?.let { host ->
            // 定义需要排除的子域名列表
            val excludedHosts = listOf("exclude.example.com", "test.example.com")
            if (excludedHosts.contains(host)) {
                // 调用系统浏览器打开链接
                val browserIntent = Intent(Intent.ACTION_VIEW, intent.data)
                browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivity(browserIntent)
                // 关闭当前Activity,避免停留在App里
                finish()
                return
            }
        }
        // 正常处理允许的域名跳转逻辑
        setupNormalPage()
    }
    

    这个方法的优势是灵活,不管多少子域名都能 handle,但要注意:如果你的 App 还没通过 App Links 验证,用户点击链接时可能会先弹出选择器,选了你的 App 之后才会触发拦截;如果已经通过验证,会直接跳转到 App 再执行拦截逻辑。

给你提个小提醒:测试的时候一定要覆盖各种场景,比如从系统浏览器、第三方 App 分享、短信里点击链接的情况,确保排除逻辑都能正常生效。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 13:07:58