<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 chính xác Intent có thể bao gồm các tham số truy vấn URI và đoạn mã URI. Các quy tắc có thể là quy tắc bao gồm (cho phép) hoặc quy tắc loại trừ (chặn) quy tắc, 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 path*, fragment*, và query* thuộc tính của các phần tử <data> được chứa.

So khớp

Để so khớp một URI, mỗi phần của nhóm bộ lọc tương đối URI phải khớp với một phần của URI. Có thể có những phần của URI không được chỉ định trong nhóm bộ lọc tương đối 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 này khớp với https://project.example.com/any/path/here?param1=value1&param2=value2&param3=value3 vì mọi thứ do nhóm bộ lọc tương đối 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 này không khớp với https://project.example.com/any/path/here?param1=value1 (thiếu param2=value2).

OR và AND

Các thẻ <data> bên ngoài <uri-relative-filter-group> được ORed, trong khi các thẻ <data> bên trong <uri-relative-filter-group> được ANDed.

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 path thuộc tính 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 này khớp với đoạn mã #fragment vì một kết quả trùng 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 mã như #fragment123 không khớp.

Thẻ ngang hàng

<uri-relative-filter-group> Các thẻ hoạt động cùng với các thẻ ngang hàng <data> (tức là các thẻ <data> nằm bên ngoài <uri-relative-filter-group> nhưng bên trong cùng một <intent-filter>). Các thẻ <uri-relative-filter-group> phải có các thẻ <data> ngang hàng để 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 một <intent-filter> được đánh giá trước mọi <uri-relative-filter-group> thẻ. 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 này 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 bên 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 một Intent tuỳ thuộc vào sự hiện diện hoặc giá trị của một 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ử 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 đang so 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 cấm tham số truy vấn không trống được đánh giá trước.

Để đơn giản hoá mã, hãy đảo ngược hành vi để cho phép các tham số truy vấn và chặn các URI 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 được 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 này khớp với ?param=value! ?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 này không khớp với ?param=value! cũng như ?param=value%21.

Số lượng phần tử

Bạn có thể đặt bất kỳ số lượng <uri-relative-filter-group> phần tử 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ư Bộ lọc ý định.

Để biết thông tin về <uri-relative-filter-group>, hãy xem UriRelativeFilterGroupUriRelativeFilter.

thuộc tính:
android:allow
Liệu nhóm bộ lọc tương đối URI này có phải là quy tắc bao gồm (cho phép) thay vì quy tắc loại trừ (chặn) hay không. 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 URI khớp, thì bộ lọc ý định sẽ khớp
"false" Nếu nhóm bộ lọc tương đối URI khớp, thì bộ lọc ý định không khớp
ra mắt từ:
cấp độ API 35
xem thêm:
<intent-filter>
<data>