- ไวยากรณ์:
-
<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>ย่อยของ<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>กรณีการใช้งานทั่วไป
สมมติว่าคุณมี 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>
แต่วิธีนี้ไม่ได้ผล URI
https://project.example.com/path?queryจับคู่กับเส้นทาง/pathและแท็ก<uri-relative-filter-group>อนุญาตให้มีชิ้นส่วนเพิ่มเติมเมื่อทำการจับคู่แก้ไขตัวกรอง 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 - หากไม่ได้ระบุ
- แอตทริบิวต์
-
android:allow-
กลุ่มตัวกรองแบบสัมพัทธ์ของ URI นี้เป็นกฎการรวม (อนุญาต) ไม่ใช่กฎการยกเว้น (การบล็อก) หรือไม่ ค่าเริ่มต้นคือ
"true"ค่า คำอธิบาย "true"(ค่าเริ่มต้น)หากกลุ่มตัวกรองแบบสัมพัทธ์ของ URI ตรงกัน ตัวกรอง Intent ก็จะตรงกัน "false"หากกลุ่มตัวกรองแบบสัมพัทธ์ของ URI ตรงกัน ตัวกรอง Intent จะไม่ตรงกัน
- เปิดตัวใน
- API ระดับ 35
- ดูข้อมูลเพิ่มเติมได้ที่
-
<intent-filter><data>
<uri-relative-filter-group>
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[]]