<uri-relative-filter-group>

語法:
<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&param2=value2&param3=value3,因為 URI 相對篩選器群組指定的所有項目都存在。由於查詢參數的順序不重要,因此篩選器也會比對 https://project.example.com/any/path/here?param2=value2&param1=value1。不過,篩選器不符合 https://project.example.com/any/path/here?param1=value1,因為缺少 param2=value2

OR 和 AND

<data> 標記 位於 <uri-relative-filter-group> 之外時,會以 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> 層級是相互依存的:

  • 如未為意圖篩選器指定 scheme ,系統將忽略所有其他 URI 屬性。
  • 如果未指定篩選器的 host ,則系統會忽略 port 屬性和所有 path* 屬性。

系統會先評估 <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" />,且不在 <uri-relative-filter-group> 排除規則的範圍內。

常見用途

假設您有 URI https://project.example.com/path,並想根據查詢參數是否存在或值為何,將其比對至 Intent。如要建立符合 https://project.example.com/path 並封鎖 https://project.example.com/path?query 的意圖篩選器,可以嘗試下列做法:

<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?query URI 會比對 /path 路徑,而 <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: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> 元素。

其他資源

如需瞭解意圖篩選器的運作方式 (包括意圖物件如何與篩選器比對的規則),請參閱「意圖和意圖篩選器」以及「意圖篩選器」。

如要瞭解 <uri-relative-filter-group>,請參閱 UriRelativeFilterGroupUriRelativeFilter

屬性:
android:allow
這個 URI 相對篩選器群組是包含 (允許) 規則,還是排除 (封鎖) 規則。預設值為 "true"
說明
"true" (預設) 如果 URI 相對篩選器群組相符,意圖篩選器就會相符
"false" 如果 URI 相對篩選器群組相符,意圖篩選器就不相符
導入版本:
API 級別 35
另請參閱:
<intent-filter>
<data>