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

Android Manifest中如何简化同一intent-filter下的Dynamic Links列表?

简化Firebase Dynamic Links的Activity Intent Filter配置

在使用Firebase Dynamic Links时,指定单个Activity处理多个动态链接,但链接仅末尾数字参数不同,当前在Android Manifest中通过重复添加<data>标签实现,计划扩展至约50个链接,希望简化配置而非持续添加重复项。原Manifest代码如下:

<activity
    android:name="com.example.app.main.HNSPS1"
    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"/>
        <!--Drinks-CoffeeShops-->
        <data android:host="myapp.page.link" android:scheme="https" android:path="/DrinksCoffeeShops/1/" />
        <data android:host="myapp.page.link" android:scheme="https" android:path="/DrinksCoffeeShops/2/" />
        <data android:host="myapp.page.link" android:scheme="https" android:path="/DrinksCoffeeShops/3/" />
        <!-- 更多重复的<data>标签... -->
    </intent-filter>
</activity>

解决方案:使用路径通配符替代重复标签

1. 用pathPattern匹配任意数字结尾的路径

Android的Intent Filter支持使用pathPattern进行正则风格的路径匹配,只需保留一个<data>标签即可匹配所有末尾为数字的链接:

<activity
    android:name="com.example.app.main.HNSPS1"
    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"/>
        <!-- 匹配/DrinksCoffeeShops/任意数字/的路径 -->
        <data 
            android:host="myapp.page.link" 
            android:scheme="https" 
            android:pathPattern="/DrinksCoffeeShops/[0-9]+/" />
    </intent-filter>
</activity>
  • [0-9]+表示匹配一个或多个数字,能覆盖1到50(甚至更大的数字)的所有情况;
  • 如果路径末尾的斜杠可能存在或缺失,可以调整为android:pathPattern="/DrinksCoffeeShops/[0-9]+/*",允许末尾有0个或多个斜杠。

2. 在Activity中提取数字参数

配置完成后,在HNSPS1Activity中可以通过Intent获取链接中的数字参数:

Java示例:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 获取Intent中的Uri
    Uri deepLinkUri = getIntent().getData();
    if (deepLinkUri != null) {
        // 拆分路径片段
        List<String> pathSegments = deepLinkUri.getPathSegments();
        // 路径为/DrinksCoffeeShops/1/,所以pathSegments的第二个元素是数字(索引从0开始)
        if (pathSegments.size() >= 2) {
            String shopId = pathSegments.get(1);
            // 处理shopId,比如转为整数
            int id = Integer.parseInt(shopId);
            // 后续业务逻辑...
        }
    }
}

Kotlin示例:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    intent.data?.let { deepLinkUri ->
        val pathSegments = deepLinkUri.pathSegments
        if (pathSegments.size >= 2) {
            val shopId = pathSegments[1]
            val id = shopId.toInt()
            // 后续业务逻辑...
        }
    }
}

额外说明

  • 如果需要限制数字范围(比如仅1-50),无需在Manifest中配置,直接在Activity中对提取到的数字进行判断即可;
  • pathPattern的匹配规则遵循Android的正则简化语法,*匹配任意字符序列,[0-9]匹配单个数字,+表示匹配前面的元素一次或多次。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 07:03:19