The document has been well understood: http://developer.android.com/reference/android/content/UriMatcher.html
Utility Class to aid in matching URIs in content providers.
To use this class, build up a treeUrimatcher
Objects. For example:
Private Static final int people = 1; Private Static final int people_id = 2; Private Static final int people_phones = 3; Private Static final int lele_phones_id = 4; Private Static final int people_contactmethods = 7; private Static final int lele_contactmethods_id = 8; Private Static final int deleted_people = 20; Private Static final int phones = 9; Private Static final int phones_id = 10; Private Static final int phones_filter = 14; private Static final int contactmethods = 18; Private Static final int contactmethods_id = 19; Private Static final int CALS = 11; Private Static final int calls_id = 12; Private Static final int calls_filter = 15; private Static final urimatcher surimatcher = new urimatcher (urimatcher. no_match); static {surimatcher. adduri ("contacts", "people", people); surimatcher. adduri ("contacts", "People/#", people_id); surimatcher. adduri ("contacts", "People/#/phones", people_phones); surimatcher. adduri ("contacts", "People/#/phones/#", lele_phones_id); surimatcher. adduri ("contacts", "People/#/contact_methods", lele_contactmethods); surimatcher. adduri ("contacts", "People/#/contact_methods/#", lele_contactmethods_id); surimatcher. adduri ("contacts", "deleted_people", deleted_people); surimatcher. adduri ("contacts", "phones", phones); surimatcher. adduri ("contacts", "phones/filter/*", phones_filter); surimatcher. adduri ("contacts", "phones/#", phones_id); surimatcher. adduri ("contacts", "contact_methods", contactmethods); surimatcher. adduri ("contacts", "contact_methods/#", contactmethods_id); surimatcher. adduri ("call_log", "CILS", CILS); surimatcher. adduri ("call_log", "CILS/filter/*", calls_filter); surimatcher. adduri ("call_log", "CILS/#", calls_id );}
Then when you need to match against a URI, callMatch (URI)
, Providing the URL that you have been given. you can use the result to build a query, return a type, insert or delete a row, or whatever you need, without duplicating all of the IF-else logic that you wowould otherwise need. for example:
Public String GetType (uri url) {int match = surimatcher. match (URL); Switch (MATCH) {Case people: Return "Vnd. android. cursor. DIR/person "; Case people_id: Return" Vnd. android. cursor. item/person ";... snip... return "Vnd. android. cursor. DIR/snil-mail "; Case people_address_id: Return" Vnd. android. cursor. item/snil-mail "; default: return NULL ;}}
Instead:
Public String GetType (uri url) {list pathsegments = URL. getpathsegments (); If (pathsegments. size ()> = 2) {If ("people ". equals (pathsegments. get (1) {If (pathsegments. size () = 2) {return "Vnd. android. cursor. DIR/person ";} else if (pathsegments. size () = 3) {return "Vnd. android. cursor. item/person ";... snip... return "Vnd. android. cursor. DIR/snil-mail ";} else if (pathsegments. size () = 3) {return "Vnd. android. cursor. item/snil-mail ";}} return NULL ;}