<uri-relative-filter-group>

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 Intent chí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ính android:allow. Các quy tắc so khớp được chỉ định bằng các thuộc tính path*, 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&param2=value2&param3=value3 vì 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ới https://project.example.com/any/path/here?param2=value2&param1=value1 vì 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ới https://project.example.com/any/path/here?param1=value1, tức là thiếu param2=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 /prefix HOẶC kết thúc bằng suffix.

Ngược lại, ví dụ tiếp theo khớp với các đường dẫn bắt đầu bằng /prefix VÀ kết thúc bằng 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>

Do đó, nhiều thuộc tính path trong 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 #fragment vì 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ư #fragment123 khô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 scheme cho bộ lọc ý định thì mọi thuộc tính URI khác sẽ bị bỏ qua.
  • Nếu không chỉ định host cho bộ lọc thì thuộc tính port và mọi thuộc tính path* 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?query vì 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/path mà bạn muốn so khớp với Intent tuỳ 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ới https://project.example.com/path và chặn https://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?query khớp với đường dẫn /path và 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 xem UriRelativeFilterGroup và UriRelativeFilter.

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>