[Android] Detailed explanation of Uri, UriMatcher, and ContentUris

 1.Uri

Universal Resource Identifier ("URI" for short).

Uri represents the data to be manipulated, and every resource available on Android - images, video clips, etc. can be represented by Uri.

 

A URI generally consists of three parts:

A naming mechanism for accessing resources. 

The hostname where the resource is stored. 

The name of the resource itself, represented by a path. 

 

 

 

 

 

Android's Uri consists of the following three parts: "content://", data path, and mark ID (optional)

Some examples like: 

Uri for all contacts: content://contacts/people

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 Uri and get data from Uri.

The Android system provides two tool classes for manipulating Uri, namely UriMatcher and ContentUris.

Although these two categories are not very important, mastering their use will facilitate our development work.

Let's take a look at the role of these two classes together.

 

 

2.UriMatcher

The UriMatcher class is mainly used to match Uri.

 

The method of use is as follows.

The first step is to initialize:

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

 

 

 

The third part, matching with the registered Uri:

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

 

 

After the match method matches, it will return a match code Code, which is the third parameter passed in when using the registration method addURI.

 

The above method will return "vnd.android.cursor.dir/person".

 

Summarize: 

--Constant UriMatcher.NO_MATCH indicates the return code that does not match any path

--# is a wildcard

--* is any character

 

 

In addition, the registration of Uri in the official SDK description 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.     }  

 

 

 

This description is estimated that Google has not officially updated, the first is the initialization method, no parameters are passed, so now when initializing, it is actually necessary to pass parameters. You can take a look at the source code of Android 2.2. The parameter-free construction method is already private.

In addition, the method of addURI does not need "/" at the beginning of the second parameter, otherwise it will not match successfully.

 

3.ContentUris

The ContentUris class is used to get the ID part behind the Uri path

1) Add an ID to the path: withAppendedId(uri, id)

For example, there is such a Uri

  1. Uri uri = Uri.parse("content://com.yfz.Lesson/people")  

 

 

通过withAppendedId方法,为该Uri加上ID

  1. Uri resultUri = ContentUris.withAppendedId(uri, 10);  

 

 

最后resultUri为: content://com.yfz.Lesson/people/10

 

2)从路径中获取ID: parseId(uri)

  1. Uri uri = Uri.parse("content://com.yfz.Lesson/people/10")  
  2. long personid = ContentUris.parseId(uri);  

 

 

最后personid 为 :10

 

附上实验的代码:

  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<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a>.Uri;  
  7. import android.os.Bundle;  
  8. public class Lesson_14 extends Activity {  
  9.           
  10.         private static final String AUTHORITY = "com.yfz.Lesson";  
  11.         private static final int PEOPLE = 1;  
  12.         private static final int PEOPLE_ID = 2;  
  13.           
  14.         //NO_MATCH表示不匹配任何路径的返回码  
  15.         private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);  
  16.         static  
  17.         {  
  18.             sURIMatcher.addURI(AUTHORITY, "people", PEOPLE);  
  19.               
  20.             //这里的#代表匹配任意数字,另外还可以用*来匹配任意文本  
  21.             sURIMatcher.addURI(AUTHORITY, "people/#", PEOPLE_ID);  
  22.         }  
  23.           
  24.         @Override  
  25.         protected void onCreate(Bundle savedInstanceState) {  
  26.             super.onCreate(savedInstanceState);  
  27.             Logger.d("------ Start Activity !!! ------");  
  28.               
  29.             Uri uri1 = Uri.parse("content://" + AUTHORITY + "/people");  
  30.             Logger.e("Uri:" + uri1);  
  31.             Logger.d("Match 1" + getType(uri1));  
  32.               
  33.             Uri uri2 = Uri.parse("content://" + AUTHORITY + "/people" + "/2");  
  34.               
  35.             Logger.e("Uri:" + uri2);  
  36.             Logger.d("Match 2" + getType(uri2));  
  37.               
  38.             //拼接Uri  
  39.             Uri cUri = ContentUris.withAppendedId(uri1, 15);  
  40.             Logger.e("Uri:" + cUri);  
  41.             //获取ID  
  42.             long id = ContentUris.parseId(cUri);  
  43.             Logger.d("Uri ID: " + id);  
  44.         }  
  45.           
  46.         private String getType(Uri uri) {  
  47.             int match = sURIMatcher.match(uri);  
  48.             switch (match)  
  49.             {  
  50.                 case PEOPLE:  
  51.                     return "vnd.android.cursor.dir/person";  
  52.                 case PEOPLE_ID:  
  53.                     return "vnd.android.cursor.item/person";  
  54.                 default:  
  55.                     return null;  
  56.             }  
  57.         }  
  58. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326423328&siteId=291194637