- 语法:
-
<uri-relative-filter-group android:allow=["true" | "false"]> <data ... /> ... </uri-relative-filter-group>
- 包含于:
-
<intent-filter> - 可包含:
-
<data> - 说明:
-
创建精确的
Intent匹配规则,可以包含 URI 查询参数和 URI 片段。规则可以是包含(允许)规则,也可以是排除(屏蔽)规则,具体取决于android:allow属性。 匹配规则由所含<data>元素的path*、fragment*和query*属性指定。匹配
若要匹配 URI,URI 相对过滤条件组的每个部分都必须与 URI 的一部分相匹配。URI 中可能存在未在 URI 相关过滤条件组中指定的部分。 例如:
<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group android:allow="true"> <data android:query="param1=value1" /> <data android:query="param2=value2" /> </uri-relative-filter-group> ... </intent-filter>
过滤条件匹配
https://project.example.com/any/path/here?param1=value1¶m2=value2¶m3=value3,因为 URI 相对过滤条件组指定的所有内容都存在。该过滤条件还会匹配https://project.example.com/any/path/here?param2=value2¶m1=value1,因为查询参数的顺序无关紧要。 不过,该过滤条件与缺少param2=value2的https://project.example.com/any/path/here?param1=value1不匹配。OR 和 AND
<uri-relative-filter-group>之外的<data>标记会进行 OR 运算,而<uri-relative-filter-group>内的<data>标记会进行 AND 运算。请参考以下示例:
<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <data android:pathPrefix="/prefix" /> <data android:pathSuffix="suffix" /> ... </intent-filter>
该过滤条件匹配以
/prefix开头或以suffix结尾的路径。相比之下,以下示例匹配以
/prefix开头且以suffix结尾的路径:<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group> <data android:pathPrefix="/prefix" /> <data android:pathSuffix="suffix" /> </uri-relative-filter-group> ... </intent-filter>
因此,同一
<uri-relative-filter-group>中的多个path属性不会匹配任何内容:<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group> <data android:path="/path1" /> <data android:path="/path2" /> </uri-relative-filter-group> ... </intent-filter>
声明顺序
请参考以下示例:
<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group> <data android:fragment="fragment" /> </uri-relative-filter-group> <uri-relative-filter-group android:allow="false"> <data android:fragmentPrefix="fragment" /> </uri-relative-filter-group> ... </intent-filter>
过滤条件与片段
#fragment匹配,因为在评估排除规则之前找到了匹配项,但片段(例如#fragment123)不匹配。同级代码
<uri-relative-filter-group>标记与其同级<data>标记(即位于<uri-relative-filter-group>之外但位于同一<intent-filter>内的<data>标记)协同工作。<uri-relative-filter-group>标记必须具有同级<data>标记才能正常运行,因为 URI 属性在<intent-filter>级别上是相互依赖的:<intent-filter>的<data>子元素会在所有<uri-relative-filter-group>标记之前进行评估。 然后,系统会按顺序评估<uri-relative-filter-group>标记,例如:<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group android:allow="false"> <data android:path="/path" /> <data android:query="query" /> </uri-relative-filter-group> <data android:path="/path" /> ... </intent-filter>
该过滤条件接受
https://project.example.com/path?query,因为它与<data android:path="/path" />匹配,而<data android:path="/path" />不在<uri-relative-filter-group>排除规则范围内。常见使用场景
假设您有一个 URI
https://project.example.com/path,您希望根据查询参数的存在情况或值将其与Intent相匹配。 如需创建与https://project.example.com/path匹配并屏蔽https://project.example.com/path?query的 intent 过滤器,您可以尝试以下操作:<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group android:allow="true"> <data android:path="/path" /> </uri-relative-filter-group> ... </intent-filter>
事实上,这行不通。
https://project.example.com/path?queryURI 匹配路径/path,而<uri-relative-filter-group>标记允许在匹配时包含额外的部分。按如下方式修改 intent 过滤器:
<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group android:allow="false"> <data android:path="/path" /> <data android:queryAdvancedPattern=".+" /> </uri-relative-filter-group> <uri-relative-filter-group android:allow="true"> <data android:path="/path" /> </uri-relative-filter-group> ... </intent-filter>
此过滤条件之所以有效,是因为系统会先评估禁止使用非空查询参数的屏蔽规则。
为了简化代码,请翻转行为,以允许使用查询参数并阻止不带查询参数的 URI:
<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group android:allow="true"> <data android:path="/path" /> <data android:queryAdvancedPattern=".+" /> </uri-relative-filter-group> ... </intent-filter>
经过 URI 编码的字符
如需匹配包含 URI 编码字符的 URI,请在过滤条件中写入原始的未编码字符,例如:
<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group android:allow="true"> <data android:query="param=value!" /> </uri-relative-filter-group> ... </intent-filter>
该过滤条件与
?param=value!和?param=value%21匹配。不过,如果您在过滤条件中写入编码后的字符,如下所示:
<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <uri-relative-filter-group android:allow="true"> <data android:query="param=value%21" /> </uri-relative-filter-group> ... </intent-filter>
过滤条件与
?param=value!和?param=value%21都不匹配。元素数量
您可以在
<intent-filter>内放置任意数量的<uri-relative-filter-group>元素。其他资源
如需了解 intent 过滤器的工作原理,包括如何根据过滤器来匹配 intent 对象的规则,请参阅 intent 和 intent 过滤器以及 intent 过滤器。
如需了解
<uri-relative-filter-group>,请参阅UriRelativeFilterGroup和UriRelativeFilter。 - 属性:
- 引入于:
- API 级别 35
- 另请参阅:
-
<intent-filter><data>
<uri-relative-filter-group>
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2026-04-21。
[null,null,["最后更新时间 (UTC):2026-04-21。"],[],[]]