[Android] Uri, UriMatcher, and ContentUris

Source: Internet
Author: User


1. Uri
The Universal Resource Identifier ("URI ").
Uri indicates the data to be operated. Each type of resource available on Android-images and video clips can be represented by Uri.
 
A URI consists of three parts:
The naming mechanism for accessing resources.
Host Name for storing resources.
The name of the resource, represented by the path.
 
 
 
 
Android Uri consists of the following three parts: "content: //", data path, and ID (optional)
For example:
Uri: content: // contacts/people for all contacts
Uri of a contact: content: // contacts/people/5
All images Uri: content: // media/external
Uri of an image: content: // media/external/images/media/4
 
 
 
 
We often need to parse the Uri and obtain data from the Uri.
The Android system provides two tool classes for Uri operations: UriMatcher and ContentUris.
Although these two types are not very important, understanding their usage will facilitate our development work.
Let's take a look at the functions of these two classes.
 
 
2. UriMatcher
The UriMatcher class is mainly used to match the Uri.
 
The usage is as follows.
First, initialize:
[Java]
UriMatcher = new UriMatcher (UriMatcher. NO_MATCH );
 
Step 2 register the required Uri:
[Java]
Matcher. addURI ("com. yfz. Lesson", "people", PEOPLE );
Matcher. addURI ("com. yfz. Lesson", "person/#", PEOPLE_ID );
 
 
Third, match with the registered Uri:
[Java]
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;
}
 
After the match method matches, a matching Code is returned, that is, the third parameter passed in when the registration method addURI is used.
 
The above method will return "vnd. android. cursor. dir/person ".
 
Summary:
-- Constant UriMatcher. NO_MATCH indicates that the return code does not match any path.
-- # It is a wildcard
-- * Indicates any character
 
 
In addition, the registration of Uri in the official SDK description is written as follows:
[Java]
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", "/CILS", CILS );
SURIMatcher. addURI ("call_log", "/CILS/filter/*", CALLS_FILTER );
SURIMatcher. addURI ("call_log", "/CILS/#", CALLS_ID );
}
 
 
This description is estimated to have not been updated by the Google official website. The first is the initialization method without passing parameters. Now, when initializing, it is actually necessary to pass parameters. You can check the source code of Android2.2. The construction method without parameters is private.
The addURI method does not need "/" when the second parameter starts. Otherwise, the matching will fail.
 
3. ContentUris
The ContentUris class is used to obtain the ID part after the Uri path.
1) Add ID: withAppendedId (uri, id) to the path)
For example, a Uri
[Java]
Uri uri = Uri. parse ("content: // com. yfz. Lesson/people ")
 
Use the withAppendedId method to add an ID to the Uri.
[Java]
Uri resultUri = ContentUris. withAppendedId (uri, 10 );
 
Finally, resultUri is: content: // com. yfz. Lesson/people/10.
 
2) obtain the ID: parseId (uri) from the path)
[Java]
Uri uri = Uri. parse ("content: // com. yfz. Lesson/people/10 ")
Long personid = ContentUris. parseId (uri );
 
Last personid: 10
 
The code for the experiment is attached:
[Java]
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 that the return code does not match any path
Private static final UriMatcher sURIMatcher = new UriMatcher (UriMatcher. NO_MATCH );
Static
{
SURIMatcher. addURI (AUTHORITY, "people", PEOPLE );

// # Indicates matching any number. You can also use * to match any text.
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 ));

// Splice the Uri
Uri cUri = ContentUris. withAppendedId (uri1, 15 );
Logger. e ("Uri:" + cUri );
// Obtain the ID
Long id = ContentUris. parseId (cUri );
Logger. d ("Uri ID:" + id );
}

Private String getType (Uri uri ){
Int match = sURIMatcher. match (uri );
Switch (match) www.2cto.com
{
Case PEOPLE:
Return "vnd. android. cursor. dir/person ";
Case PEOPLE_ID:
Return "vnd. android. cursor. item/person ";
Default:
Return null;
}
}
}
Author: chang_xing

Related Article

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.