- 語法:
-
<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。不過,篩選器不符合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>層級是相互依存的:系統會先評估
<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?queryURI 會比對/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>,請參閱UriRelativeFilterGroup和UriRelativeFilter。 - 屬性:
- 導入版本:
- API 級別 35
- 另請參閱:
-
<intent-filter><data>
<uri-relative-filter-group>
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2026-04-21 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2026-04-21 (世界標準時間)。"],[],[]]