- cú pháp:
-
<uri-relative-filter-group android:allow=["true" | "false"]> <data ... /> ... </uri-relative-filter-group>
- có trong:
-
<intent-filter> - có thể chứa:
-
<data> - mô tả:
-
Tạo các quy tắc so khớp
Intentchính xác có thể bao gồm các tham số truy vấn URI và đoạn URI. Các quy tắc này có thể là quy tắc chọn lọc (cho phép) hoặc quy tắc loại trừ (chặn), tuỳ thuộc vào thuộc tínhandroid:allow. Các quy tắc so khớp được chỉ định bằng các thuộc tínhpath*,fragment*vàquery*của các phần tử<data>có trong đó.Khớp
Để khớp với một URI, mỗi phần của nhóm bộ lọc tương đối của URI phải khớp với một phần của URI. Có thể có các phần của URI không được chỉ định trong nhóm bộ lọc tương đối của URI. Ví dụ:
<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>
Bộ lọc khớp với
https://project.example.com/any/path/here?param1=value1¶m2=value2¶m3=value3vì mọi thông tin do nhóm bộ lọc liên quan đến URI chỉ định đều có mặt. Bộ lọc này cũng khớp vớihttps://project.example.com/any/path/here?param2=value2¶m1=value1vì thứ tự của các tham số truy vấn không quan trọng. Tuy nhiên, bộ lọc không khớp vớihttps://project.example.com/any/path/here?param1=value1, tức là thiếuparam2=value2.OR và AND
Các thẻ
<data>bên ngoài<uri-relative-filter-group>được kết hợp bằng toán tử OR, trong khi các thẻ<data>bên trong<uri-relative-filter-group>được kết hợp bằng toán tử AND.Hãy xem ví dụ sau đây:
<intent-filter...> <data android:scheme="https" android:host="project.example.com" /> <data android:pathPrefix="/prefix" /> <data android:pathSuffix="suffix" /> ... </intent-filter>
Bộ lọc này khớp với các đường dẫn bắt đầu bằng
/prefixHOẶC kết thúc bằngsuffix.Ngược lại, ví dụ tiếp theo khớp với các đường dẫn bắt đầu bằng
/prefixVÀ kết thúc bằngsuffix:<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>
Do đó, nhiều thuộc tính
pathtrong cùng một<uri-relative-filter-group>không khớp với bất kỳ thuộc tính nào:<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>
Thứ tự khai báo
Hãy xem ví dụ sau đây:
<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>
Bộ lọc khớp với đoạn
#fragmentvì kết quả khớp được tìm thấy trước khi quy tắc loại trừ được đánh giá, nhưng các đoạn như#fragment123không khớp.Thẻ đồng cấp
<uri-relative-filter-group>hoạt động cùng với các thẻ đồng cấp<data>(tức là các thẻ<data>nằm ngoài<uri-relative-filter-group>nhưng nằm trong cùng một<intent-filter>). Thẻ<uri-relative-filter-group>phải có các thẻ đồng cấp<data>để hoạt động đúng cách vì các thuộc tính URI phụ thuộc lẫn nhau ở cấp<intent-filter>:- Nếu không chỉ định
schemecho bộ lọc ý định thì mọi thuộc tính URI khác sẽ bị bỏ qua. - Nếu không chỉ định
hostcho bộ lọc thì thuộc tínhportvà mọi thuộc tínhpath*sẽ bị bỏ qua.
Các phần tử con
<data>của<intent-filter>được đánh giá trước mọi thẻ<uri-relative-filter-group>. Sau đó, các thẻ<uri-relative-filter-group>được đánh giá theo thứ tự, ví dụ:<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>
Bộ lọc chấp nhận
https://project.example.com/path?queryvì bộ lọc này khớp với<data android:path="/path" />, nằm ngoài quy tắc loại trừ<uri-relative-filter-group>.Trường hợp sử dụng phổ biến
Giả sử bạn có URI
https://project.example.com/pathmà bạn muốn so khớp vớiIntenttuỳ thuộc vào sự hiện diện hoặc giá trị của tham số truy vấn. Để tạo một bộ lọc ý định khớp vớihttps://project.example.com/pathvà chặnhttps://project.example.com/path?query, bạn có thể thử làm như sau:<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>
Trên thực tế, cách này không hiệu quả. URI
https://project.example.com/path?querykhớp với đường dẫn/pathvà thẻ<uri-relative-filter-group>cho phép các phần bổ sung khi khớp.Sửa đổi bộ lọc ý định như sau:
<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>
Bộ lọc này hoạt động vì các quy tắc chặn không cho phép tham số truy vấn không trống được đánh giá trước tiên.
Để đơn giản hoá mã, hãy lật hành vi để cho phép các tham số truy vấn và chặn URI mà không có tham số truy vấn:
<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>
Ký tự được mã hoá URI
Để so khớp các URI chứa ký tự được mã hoá URI, hãy viết các ký tự thô, chưa mã hoá trong bộ lọc, ví dụ:
<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>
Bộ lọc khớp với
?param=value!và?param=value%21.Tuy nhiên, nếu bạn viết các ký tự được mã hoá trong bộ lọc như sau:
<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>
Bộ lọc không khớp với
?param=value!hoặc?param=value%21.Số phần tử
Bạn có thể đặt bất kỳ số lượng phần tử
<uri-relative-filter-group>nào vào bên trong<intent-filter>.Tài nguyên khác
Để biết thông tin về cách hoạt động của bộ lọc ý định, bao gồm cả quy tắc về cách so khớp đối tượng ý định với bộ lọc, hãy xem bài viết Ý định và bộ lọc ý định cũng như phần Bộ lọc ý định.
Để biết thông tin về
<uri-relative-filter-group>, hãy xemUriRelativeFilterGroupvàUriRelativeFilter. - Nếu không chỉ định
- thuộc tính:
-
android:allow-
Liệu nhóm bộ lọc tương đối theo URI này là quy tắc bao gồm (cho phép) hay quy tắc loại trừ (chặn). Giá trị mặc định là
"true".Giá trị Mô tả "true"(mặc định)Nếu nhóm bộ lọc tương đối của URI khớp, thì bộ lọc ý định sẽ khớp "false"Nếu nhóm bộ lọc tương đối của URI khớp, thì bộ lọc ý định sẽ không khớp
- ra mắt từ:
- cấp độ API 35
- xem thêm:
-
<intent-filter><data>
<uri-relative-filter-group>
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2026-12-01 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2026-12-01 UTC."],[],[]]