1.Urijava
通用資源標誌符(Universal Resource Identifier, 簡稱"URI")。android
Uri表明要操做的數據,Android上可用的每種資源 - 圖像、視頻片斷等均可以用Uri來表示。app
URI通常由三部分組成:ide
訪問資源的命名機制。 工具
存放資源的主機名。 spa
資源自身的名稱,由路徑表示。 .net
Android的Uri由如下三部分組成: "content://"、數據的路徑、標示ID(可選)視頻
舉些例子,如: blog
全部聯繫人的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.
使用方法以下。
首先第一步,初始化:
[java] view plaincopyprint?
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
第二步註冊須要的Uri:
[java] view plaincopyprint?
matcher.addURI("com.yfz.Lesson", "people", PEOPLE);
matcher.addURI("com.yfz.Lesson", "person/#", PEOPLE_ID);
第三部,與已經註冊的Uri進行匹配:
[java] view plaincopyprint?
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;
}
match方法匹配後會返回一個匹配碼Code,即在使用註冊方法addURI時傳入的第三個參數。
上述方法會返回"vnd.android.cursor.dir/person".
總結:
--常量 UriMatcher.NO_MATCH 表示不匹配任何路徑的返回碼
--# 號爲通配符
--* 號爲任意字符
另外說一下,官方SDK說明中關於Uri的註冊是這樣寫的:
[java] view plaincopyprint?
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);
}
這個說明估計已是Google官方沒有更新,首先是初始化方法,沒有傳參,那麼如今初始化時,實際是必須傳參的。 能夠看一下Android2.2的源碼,無參數的構造方法已是private的了。
另外就是addURI這個方法,第二個參數開始時不須要"/", 不然是沒法匹配成功的。
3.ContentUris
ContentUris 類用於獲取Uri路徑後面的ID部分
1)爲路徑加上ID: withAppendedId(uri, id)
好比有這樣一個Uri
[java] view plaincopyprint?
Uri uri = Uri.parse("content://com.yfz.Lesson/people")
經過withAppendedId方法,爲該Uri加上ID
[java] view plaincopyprint?
Uri resultUri = ContentUris.withAppendedId(uri, 10);
最後resultUri爲: content://com.yfz.Lesson/people/10
2)從路徑中獲取ID: parseId(uri)
[java] view plaincopyprint?
Uri uri = Uri.parse("content://com.yfz.Lesson/people/10")
long personid = ContentUris.parseId(uri);
最後personid 爲 :10
附上實驗的代碼:
[java] view plaincopyprint?
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表示不匹配任何路徑的返回碼
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static
{
sURIMatcher.addURI(AUTHORITY, "people", PEOPLE);
//這裏的#表明匹配任意數字,另外還能夠用*來匹配任意文本
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));
//拼接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;
}
}
}
好了,就講這麼多。