Android Uri, Urimatcher, Contenturis detailed

Source: Internet
Author: User

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:

    1. Urimatcher matcher = new Urimatcher (urimatcher.no_match);
Urimatcher matcher = new Urimatcher (urimatcher.no_match);

The second step is to register the required URI:

    1. Matcher.adduri ("Com.yfz.Lesson", "people", people);
    2. 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:

  1. Uri uri = uri.parse ("content://" + "Com.yfz.Lesson" + "/people");
  2. int match = Matcher.match (URI);
  3. Switch (match)
  4. {
  5. Case people:
  6. return "Vnd.android.cursor.dir/people";
  7. Case PEOPLE_ID:
  8. return "Vnd.android.cursor.item/people";
  9. Default
  10. return null;
  11. }
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:

  1. private static final Urimatcher Surimatcher = new Urimatcher ();
  2. Static
  3. {
  4. Surimatcher.adduri ("Contacts", "/people", people);
  5. Surimatcher.adduri ("Contacts", "/people/#", people_id);
  6. Surimatcher.adduri ("Contacts", "/people/#/phones", people_phones);
  7. Surimatcher.adduri ("Contacts", "/people/#/phones/#", people_phones_id);
  8. Surimatcher.adduri ("Contacts", "/people/#/contact_methods", people_contactmethods);
  9. Surimatcher.adduri ("Contacts", "/people/#/contact_methods/#", people_contactmethods_id);
  10. Surimatcher.adduri ("Contacts", "/deleted_people", deleted_people);
  11. Surimatcher.adduri ("Contacts", "/phones", phones);
  12. Surimatcher.adduri ("Contacts", "/phones/filter/*", Phones_filter);
  13. Surimatcher.adduri ("Contacts", "/phones/#", phones_id);
  14. Surimatcher.adduri ("Contacts", "/contact_methods", contactmethods);
  15. Surimatcher.adduri ("Contacts", "/contact_methods/#", contactmethods_id);
  16. Surimatcher.adduri ("Call_log", "/calls", calls);
  17. Surimatcher.adduri ("Call_log", "/calls/filter/*", Calls_filter);
  18. Surimatcher.adduri ("Call_log", "/calls/#", calls_id);
  19. }
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

    1. 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

    1. 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

    1. Uri uri = uri.parse ("CONTENT://COM.YFZ.LESSON/PEOPLE/10")
    2. 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:

  1. Package COM.YFZ;
  2. Import Com.yfz.log.Logger;
  3. Import android.app.Activity;
  4. Import Android.content.ContentUris;
  5. Import Android.content.UriMatcher;
  6. Import Android.net.Uri;
  7. Import Android.os.Bundle;
  8. public class Lesson_14 extends Activity {
  9. Private static final String authority = "Com.yfz.Lesson";
  10. private static final int people = 1;
  11. private static final int people_id = 2;
  12. No_match indicates a return code that does not match any path
  13. private static final Urimatcher Surimatcher = new Urimatcher (urimatcher.no_match);
  14. Static
  15. {
  16. Surimatcher.adduri (Authority, "people", people);
  17. The # symbol here matches any number, and you can match any text with *
  18. Surimatcher.adduri (Authority, "people/#", people_id);
  19. }
  20. @Override
  21. protected void OnCreate (Bundle savedinstancestate) {
  22. Super.oncreate (savedinstancestate);
  23. LOGGER.D ("------Start Activity!!! ------");
  24. Uri Uri1 = Uri.parse ("content://" + Authority + "/people");
  25. LOGGER.E ("Uri:" + uri1);
  26. LOGGER.D ("Match 1" + getType (URI1));
  27. Uri uri2 = Uri.parse ("content://" + Authority + "/people" + "/2");
  28. LOGGER.E ("Uri:" + uri2);
  29. LOGGER.D ("Match 2" + getType (URI2));
  30. Stitching URI
  31. Uri Curi = Contenturis.withappendedid (Uri1, 15);
  32. LOGGER.E ("Uri:" + Curi);
  33. Get ID
  34. Long id = Contenturis.parseid (curi);
  35. LOGGER.D ("Uri ID:" + ID);
  36. }
  37. Private String GetType (Uri uri) {
  38. int match = Surimatcher.match (URI);
  39. Switch (match)
  40. {
  41. Case people:
  42. return "Vnd.android.cursor.dir/person";
  43. Case PEOPLE_ID:
  44. return "Vnd.android.cursor.item/person";
  45. Default
  46. return null;
  47. }
  48. }
  49. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.