1.Uri
Generic resource Identifier (Universal Resource Identifier, referred to as "URI").
URIs represent the data to be manipulated, and each resource available on Android-images, video clips, and so on-can be represented by URIs.
URIs are generally made up of three parts:
The naming mechanism for accessing resources.
Host name of the storage resource.
The name of the resource itself, represented by the path.
The URI of Android consists of the following three parts: "content://", the path of the data, the ID of the indicator (optional)
For example, such as:
Uri:content://contacts/people of all contact persons
The URI:CONTENT://CONTACTS/PEOPLE/5 of a contact.
All pictures uri:content://media/external
The URI:CONTENT://MEDIA/EXTERNAL/IMAGES/MEDIA/4 of a picture
We often need to parse the URI and get the data from the URI.
The Android system provides two tool classes for manipulating URIs, Urimatcher and Contenturis, respectively.
While these two classes are not very important, mastering their use will facilitate our development efforts.
Let's take a look at the effects of these two classes.
2.UriMatcher
The Urimatcher class is primarily used to match URIs.
Use the following method.
First step, Initialize:
- Urimatcher matcher = new Urimatcher (urimatcher.no_match);
Urimatcher matcher = new Urimatcher (urimatcher.no_match);
The second step is to register the required URI:
- Matcher.adduri ("Com.yfz.Lesson", "people", people);
- Matcher.adduri ("Com.yfz.Lesson", "person/#", people_id);
Matcher.adduri ("Com.yfz.Lesson", "people", people); Matcher.adduri ("Com.yfz.Lesson", "person/#", people_id);
The third part matches the URI that is already registered:
- Uri uri = uri.parse ("content://" + "Com.yfz.Lesson" + "/people");
- int match = Matcher.match (URI);
- Switch (match)
- {
- Case people:
- return "Vnd.android.cursor.dir/people";
- Case PEOPLE_ID:
- return "Vnd.android.cursor.item/people";
- Default
- return null;
- }
Uri uri = uri.parse ("content://" + "Com.yfz.Lesson" + "/people"); int match = Matcher.match (URI); Switch (Match) {case people: return ' vnd.android.cursor.dir/people '; Case PEOPLE_ID: return "Vnd.android.cursor.item/people"; Default: return null; }
The match method will return a match code code, which is the third parameter passed in when the registration method Adduri is used.
The above method returns "Vnd.android.cursor.dir/person".
Summarize:
--Constant Urimatcher.no_match indicates a return code that does not match any path
--# number as a wildcard character
--* number is any character
In addition, the official SDK description of the URI registration is written like this:
- private static final Urimatcher Surimatcher = new Urimatcher ();
- Static
- {
- Surimatcher.adduri ("Contacts", "/people", people);
- Surimatcher.adduri ("Contacts", "/people/#", people_id);
- Surimatcher.adduri ("Contacts", "/people/#/phones", people_phones);
- Surimatcher.adduri ("Contacts", "/people/#/phones/#", people_phones_id);
- Surimatcher.adduri ("Contacts", "/people/#/contact_methods", people_contactmethods);
- Surimatcher.adduri ("Contacts", "/people/#/contact_methods/#", people_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", "/calls", calls);
- Surimatcher.adduri ("Call_log", "/calls/filter/*", Calls_filter);
- Surimatcher.adduri ("Call_log", "/calls/#", calls_id);
- }
private static final Urimatcher Surimatcher = new Urimatcher (); static {Surimatcher.adduri ("Contacts", "/people", people); Surimatcher.adduri ("Contacts", "/people/#", people_id); Surimatcher.adduri ("Contacts", "/people/#/phones", people_phones); Surimatcher.adduri ("Contacts", "/people/#/phones/#", people_phones_id); Surimatcher.adduri ("Contacts", "/people/#/contact_methods", people_contactmethods); Surimatcher.adduri ("Contacts", "/people/#/contact_methods/#", people_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", "/calls", calls); Surimatcher.adduri ("Call_log", "/calls/filter/*", Calls_filter); Surimatcher.adduri ("Call_log", "/calls/#", calls_id); }
This note is estimated that Google has not been updated officially, first of all, the initialization method, no parameters, then the current initialization, the actual must be passed. Can look at the source code of Android2.2, the construction method without parameters is already private.
Another is Adduri this method, the second parameter does not need "/" at the beginning, otherwise it is unable to match successfully.
3.ContentUris
The Contenturis class is used to get the ID part after the URI path
1) Add Id:withappendedid (URI, id) for the path
For example, there is such a URI
- Uri uri = uri.parse ("Content://com.yfz.lesson/people")
Uri uri = uri.parse ("Content://com.yfz.lesson/people")
Use the Withappendedid method to add an ID to the URI
- Uri Resulturi = Contenturis.withappendedid (URI, 10);
Uri Resulturi = Contenturis.withappendedid (URI, 10);
Last Resulturi for: CONTENT://COM.YFZ.LESSON/PEOPLE/10
2) Get Id:parseid (URI) from the path
- Uri uri = uri.parse ("CONTENT://COM.YFZ.LESSON/PEOPLE/10")
- Long PersonID = Contenturis.parseid (URI);
Uri uri = uri.parse ("CONTENT://COM.YFZ.LESSON/PEOPLE/10") long PersonID = Contenturis.parseid (URI);
Last PersonID: 10
Attach the code of the Experiment:
- Package COM.YFZ;
- Import Com.yfz.log.Logger;
- Import android.app.Activity;
- Import Android.content.ContentUris;
- Import Android.content.UriMatcher;
- Import Android.net.Uri;
- Import Android.os.Bundle;
- public class Lesson_14 extends Activity {
- Private static final String authority = "Com.yfz.Lesson";
- private static final int people = 1;
- private static final int people_id = 2;
- No_match indicates a return code that does not match any path
- private static final Urimatcher Surimatcher = new Urimatcher (urimatcher.no_match);
- Static
- {
- Surimatcher.adduri (Authority, "people", people);
- The # symbol here matches any number, and you can match any text with *
- Surimatcher.adduri (Authority, "people/#", people_id);
- }
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- LOGGER.D ("------Start Activity!!! ------");
- Uri Uri1 = Uri.parse ("content://" + Authority + "/people");
- LOGGER.E ("Uri:" + uri1);
- LOGGER.D ("Match 1" + getType (URI1));
- Uri uri2 = Uri.parse ("content://" + Authority + "/people" + "/2");
- LOGGER.E ("Uri:" + uri2);
- LOGGER.D ("Match 2" + getType (URI2));
- Stitching URI
- Uri Curi = Contenturis.withappendedid (Uri1, 15);
- LOGGER.E ("Uri:" + Curi);
- Get ID
- Long id = Contenturis.parseid (curi);
- LOGGER.D ("Uri ID:" + ID);
- }
- Private String GetType (Uri uri) {
- int match = Surimatcher.match (URI);
- Switch (match)
- {
- Case people:
- return "Vnd.android.cursor.dir/person";
- Case PEOPLE_ID:
- return "Vnd.android.cursor.item/person";
- Default
- return null;
- }
- }
- }
Package Com.yfz;import Com.yfz.log.logger;import Android.app.activity;import android.content.contenturis;import Android.content.urimatcher;import Android.net.uri;import Android.os.bundle;public class Lesson_14 extends Activity { Private static final String authority = "Com.yfz.Lesson";p rivate static final int people = 1;private static final int peop le_id = 2;//no_match Indicates a return code that does not match any path private static final Urimatcher Surimatcher = new Urimatcher (urimatcher.no_match); static {Surimatcher.adduri (authority, "people", people); The # here matches any number, and can also be used to match any text Surimatcher.adduri (authority, "people/#", people_id); } @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOGGER.D ("------Start Activity!!! ------"); Uri Uri1 = Uri.parse ("content://" + Authority + "/people"); LOGGER.E ("Uri:" + uri1); LOGGER.D ("Match 1" + getType (URI1)); Uri uri2 = Uri.parse ("content://" + Authority + "/people" + "/2"); LOGGER.E ("Uri:" + uri2); Logger.D ("Match 2" + getType (URI2));//stitching Uriuri Curi = Contenturis.withappendedid (Uri1, 15); LOGGER.E ("Uri:" + Curi);//get Idlong id = contenturis.parseid (curi); LOGGER.D ("Uri ID:" + ID);} Private String GetType (Uri uri) {int match = Surimatcher.match (URI); Switch (match) {case People:return "Vnd.android.cursor.dir/person"; Case People_id:return "Vnd.android.cursor.item/person"; Default:return null; }}
Android Uri, Urimatcher, Contenturis detailed