日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android】Uri、UriMatcher、ContentUris详解

發布時間:2025/3/21 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android】Uri、UriMatcher、ContentUris详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.Uri

通用資源標志符(Universal Resource 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.

?

使用方法如下。

首先第一步,初始化:

UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

第二步注冊需要的Uri:

matcher.addURI("com.yfz.Lesson", "people", PEOPLE); matcher.addURI("com.yfz.Lesson", "person/#", PEOPLE_ID);

第三部,與已經注冊的Uri進行匹配:

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的注冊是這樣寫的:

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

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

通過withAppendedId方法,為該Uri加上ID:

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

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

?

2)從路徑中獲取ID: parseId(uri):

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

最后personid 為 :10

?

附上實驗的代碼:

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);}@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));//拼接UriUri cUri = ContentUris.withAppendedId(uri1, 15);Logger.e("Uri:" + cUri);//獲取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;}} }

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

轉載于:https://www.cnblogs.com/MMLoveMeMM/articles/3560342.html

總結

以上是生活随笔為你收集整理的【Android】Uri、UriMatcher、ContentUris详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。