- ไวยากรณ์:
-
<uri-relative-filter-group android:allow=["true" | "false"]> <data ... /> ... </uri-relative-filter-group>
- มีอยู่ใน
-
<intent-filter> - อาจมีข้อมูลต่อไปนี้
-
<data> - description:
-
สร้างกฎการจับคู่ที่
Intentแม่นยำซึ่งอาจรวมถึงพารามิเตอร์การค้นหา URI และ ส่วนย่อย URI กฎอาจเป็นกฎการรวม (อนุญาต) หรือกฎการยกเว้น (การบล็อก) ก็ได้ ขึ้นอยู่กับแอตทริบิวต์android:allowกฎการจับคู่จะระบุโดยแอตทริบิวต์path*,fragment*และquery*ขององค์ประกอบ<data>ที่มีอยู่การจับคู่
หากต้องการจับคู่ 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=value2OR และ AND
แท็ก
<data>นอก<uri-relative-filter-group>จะใช้ OR ส่วนแท็ก<data>ภายใน<uri-relative-filter-group>จะใช้ 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>
ด้วยเหตุนี้ แอตทริบิวต์หลายรายการ
pathใน<uri-relative-filter-group>เดียวกันจึงไม่ตรงกับรายการใดเลย<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>ที่อยู่ในระดับเดียวกัน (นั่นคือแท็ก<data>ที่อยู่นอก<uri-relative-filter-group>แต่อยู่ใน<intent-filter>เดียวกัน) แท็ก<uri-relative-filter-group>ต้องมีแท็ก<data>ที่อยู่ในระดับเดียวกันจึงจะทำงานได้อย่างถูกต้อง เนื่องจากแอตทริบิวต์ URI มีความสัมพันธ์กันที่ระดับ<intent-filter>- หากไม่ได้ระบุ
schemeสำหรับตัวกรอง Intent ระบบจะละเว้นแอตทริบิวต์ URI อื่นๆ ทั้งหมด - หากไม่ได้ระบุ
hostสำหรับตัวกรอง ระบบจะไม่สนใจแอตทริบิวต์portและแอตทริบิวต์path*ทั้งหมด
ระบบจะประเมิน
<data>children ของ<intent-filter>ก่อนแท็ก<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>Use Case ทั่วไป
สมมติว่าคุณมี URI
https://project.example.com/pathซึ่งต้องการจับคู่กับIntentโดยขึ้นอยู่กับการมีอยู่หรือค่าของพารามิเตอร์การค้นหา หากต้องการสร้างตัวกรอง 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>อนุญาตให้มีส่วนextra เมื่อจับคู่แก้ไขตัวกรอง Intent ดังนี้
<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จำนวนองค์ประกอบ
คุณวางองค์ประกอบ
<uri-relative-filter-group>กี่รายการก็ได้ภายใน<intent-filter>แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเกี่ยวกับวิธีการทำงานของตัวกรอง Intent รวมถึงกฎเกี่ยวกับวิธีจับคู่ออบเจ็กต์ Intent กับตัวกรองได้ที่ Intent และตัวกรอง Intent และ ตัวกรอง Intent
ดูข้อมูลเกี่ยวกับ
<uri-relative-filter-group>ได้ที่UriRelativeFilterGroupและUriRelativeFilter - หากไม่ได้ระบุ
- แอตทริบิวต์
- เปิดตัวใน
- ระดับ API 35
- ดูเพิ่มเติม
-
<intent-filter><data>
<uri-relative-filter-group>
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2026-04-21 UTC
[null,null,["อัปเดตล่าสุด 2026-04-21 UTC"],[],[]]