1.Uri
通用资源标志符(UniversalResource Identifier, 简称"URI")。Uri代表要操作的数据,Android上可用的每种资源 - 图像、视频片段等都可以用Uri来表示。
URI一般由三部分组成:访问资源的命名机制。存放资源的主机名。资源自身的名称,由路径表示。
Android的Uri由三部分组成: content://、数据的路径、标示ID(可选)
所有联系人Uri: content://contacts/people
某个联系人Uri: content://contacts/people/5
所有图片Uri: content://media/external
某个图片的Uri: content://media/external/images/media/4
我们很经常需要解析Uri,并从Uri中获取数据。
Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris 。
2.UriMatcher
UriMatcher 类主要用于匹配Uri.
使用方法如下。
首先第一步,初始化:
<span style="font-size:12px;">UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); </span>
第二步注册需要的Uri:<span style="font-size:12px;">matcher.addURI("com.yfz.Lesson", "people", PEOPLE);
matcher.addURI("com.yfz.Lesson", "person/#", PEOPLE_ID); </span>
第三部,与已经注册的Uri进行匹配:<span style="font-size:12px;">Uri uri = Uri.parse("content://" + "com.yfz.Lesson" +"/people");
int match = matcher.match(uri);
switch (match)
{
case PEOPLE:
return "vnd.android.cursor.dir/people";
casePEOPLE_ID:
return "vnd.android.cursor.item/people";
default:
return null;
} </span>
match方法匹配后会返回一个匹配码Code,即在使用注册方法addURI时传入的第三个参数。上述方法会返回"vnd.android.cursor.dir/person".
总结:
--常量 UriMatcher.NO_MATCH表示不匹配任何路径的返回码
--# 号为通配符
--* 号为任意字符
3.ContentUris
ContentUris 类用于获取Uri路径后面的ID部分
1)为路径加上ID:withAppendedId(uri, id)
比如有这样一个Uri
<span style="font-size:12px;">Uri uri = Uri.parse("content://com.yfz.Lesson/people") </span>
通过withAppendedId方法,为该Uri加上ID<span style="font-size:12px;">Uri resultUri = ContentUris.withAppendedId(uri, 10); </span>
最后resultUri为:<span style="font-size:12px;">content://com.yfz.Lesson/people/10</span>
2)从路径中获取ID:parseId(uri)
<span style="font-size:12px;">Uri uri = Uri.parse("content://com.yfz.Lesson/people/10")
long personid = ContentUris.parseId(uri); </span>
最后personid 为 :10
3)示例代码
package com.ysu;
import com.ysu.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 StringAUTHORITY = "com.ysu.Lesson";
private static final int PEOPLE =1;
private static final int PEOPLE_ID =2;
//NO_MATCH表示不匹配任何路径的返回码
private static final UriMatchersURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static
{
sURIMatcher.addURI(AUTHORITY, "people", PEOPLE);
//这里的#代表匹配任意数字,另外还可以用*来匹配任意文本
sURIMatcher.addURI(AUTHORITY, "people/#", PEOPLE_ID);
}
@Override
protected void onCreate(BundlesavedInstanceState) {
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));
//拼接Uri
Uri cUri =ContentUris.withAppendedId(uri1, 15);
Logger.e("Uri:" + cUri);
//获取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;
}
}
}