android之ContentResolver與ContentProvider介紹
android中對數(shù)據(jù)操作包含有:
file, sqlite3, Preferences, ContectResolver與ContentProvider前三種數(shù)據(jù)操作方式都只是針對本應(yīng)用內(nèi)數(shù)據(jù),程序不能通過這三種方法去操作別的應(yīng)用內(nèi)的數(shù)據(jù)。
android中提供ContectResolver與ContentProvider來操作別的應(yīng)用程序的數(shù)據(jù)。
一、 使用方式
一個應(yīng)用實現(xiàn)ContentProvider來提供內(nèi)容給別的應(yīng)用來操作,
一個應(yīng)用通過ContentResolver來操作別的應(yīng)用數(shù)據(jù),當(dāng)然在自己的應(yīng)用中也可以。
1. ContentResolver的獲取
通過Context類:
public abstract ContentResolver getContentResolver();
2. ContentResolver常用操作
//查詢:
public final Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder);
//新增
public final Uri insert(Uri url, ContentValues values)
//更新
public final int update(Uri uri, ContentValues values, String where,
String[] selectionArgs)
//刪除
public final int delete(Uri url, String where, String[] selectionArgs)
以上操作實際是通過Uri來匹配ContentProvider, 再由ContentProvider來進行具體操作的。
操作的參數(shù)和操作sqlite各函數(shù)的參數(shù)意義是一樣的。
二、實現(xiàn)ContentProvider提供給外界訪問
調(diào)用者ContentResoler是通過一個Uri來找到相應(yīng)的ContentProvider的來進行實際操作。
1. Uri概念
一個Uri的樣子如:
scheme://authorities/path/id
如電話記錄:
public static final Uri CONTENT_URI = Uri.parse("content://call_log/calls");
a.根據(jù)scheme不同調(diào)用不程序來處理, 常用的:content, android_resource, file, http等
b.authorities是provider定義的,在AndroidManifest.xml中定義
c.path和id就好理解的。
2. Uri定義
創(chuàng)建自己的Uri, 如:
content://com.shguo.statistic/sms
一般數(shù)據(jù)中都有dir和item兩種(當(dāng)然可定義多個)。為ContentProvider創(chuàng)建息的UriMatcher并添加這兩者:
String AUTHORITY = "com.shguo.statistics";
UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(AUTHORITY, "sms", SMS_DIR); //SMS_DIR = 1
sUriMatcher.addURI(AUTHORITY, "sms/#", SMS_ITEM); //SMS_ITEM = 2
contentProvider要根據(jù)傳入uri判斷是dir還是item來操作的。
switch (sUriMatcher.match(uri))
來分步操作.
3. 定義MIME類型,
覆蓋getType方法:主要是根據(jù)uri來返回Provider的MIME類型
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.shguo.sms";
ublic static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.shguo.sms";
getType()為:
switch (sUriMatcher.match(uri)) {
case SMS_DIR:
return CONTENT_TYPE;
case SMS_ITEM:
return CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
4. 實現(xiàn)query, insert, delete, update四個操作。
具體的實現(xiàn)可以用sqlite, file等。并根據(jù)uri分情況操作。
a. query時如果是item加查詢條件id.
where = "_ID=" + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : "";
最后要加上
cursor.setNotificationUri(getContext().getContentResolver(), uri);
b. insert時要求uri只能是dir. 成功之后返回一個加id的uri.
Uri insertUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
c. update、delete與query差不多。
//注意通知注冊uri的觀察者。
getContext().getContentResolver().notifyChange(uri, null);
5. 在AndroidManifest.xml中定義
provider元素,主要屬性有:
name => ContentProvider類名
authorities => content type的授權(quán)部分
multiprocess => true允許在每個客戶進程中創(chuàng)建provider實例,消除執(zhí)行IPC的需求。
相關(guān)文章
Android通過Handler與AsyncTask兩種方式動態(tài)更新ListView(附源碼)
這篇文章主要介紹了Android通過Handler與AsyncTask兩種方式動態(tài)更新ListView的方法,結(jié)合實例形式分析了ListView動態(tài)更新的常用技巧,并附上完整實例源碼供讀者下載,需要的朋友可以參考下2015-12-12
Android后臺啟動Activity的實現(xiàn)示例
這篇文章主要介紹了Android后臺啟動Activity的實現(xiàn)示例,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04
Android自定義view實現(xiàn)標(biāo)簽欄功能(只支持固定兩個標(biāo)簽)
這篇文章主要介紹了Android自定義view實現(xiàn)標(biāo)簽欄(只支持固定兩個標(biāo)簽),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
RecycleView實現(xiàn)item側(cè)滑刪除與拖拽
這篇文章主要為大家詳細(xì)介紹了RecycleView實現(xiàn)item側(cè)滑刪除與拖拽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11
Android實現(xiàn)從緩存中讀取圖片與異步加載功能類
這篇文章主要介紹了Android實現(xiàn)從緩存中讀取圖片與異步加載功能類,涉及Android針對緩存的操作及圖片異步加載相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-08-08

