應用程式使用ContentResolver用戶端對象訪問來自用戶端的資料。這個對象有一些與提供器中相同的命名的方法,提供器是ContentProvider的一個具體的子類的執行個體。ContentResolver對象的方法提供了基本的“CRUD”(建立、擷取、更新和刪除)持久化儲存的功能。
在用戶端應用程式的進程中的ContentResolver對象和提供器自己應用中的ContentProvider對象自動的處理處理序間通訊。ContentProvider對象也以表的形式在資料資產庫和資料的外部表格現之間扮演著抽象層的角色。
注意:要訪問一個提供器,通常需要在資訊清單檔中要請求一個特殊的許可。更詳細的描述請看“內容提供器許可”
例如,要從使用者字典提供器中獲得單詞和位置的列表,你要調用ContentResolver.query()方法。Query()方法會調用由使用者字典提供器定義的ContentProvider.query()方法。以下代碼顯示了ContentResolver.query()方法的調用:
// Queries the user dictionary and returns results
mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI, // The content URI of the words table
mProjection, // The columns to return for each row
mSelectionClause // Selection criteria
mSelectionArgs, // Selection criteria
mSortOrder); // The sort order for the returned rows
表2顯示了query(Uri, projection, selection, selectionArgs, sortOrder)方法的參數是如何跟SQL的Select語句進行匹配的:
表2:query()方法跟SQL查詢比較。
query()方法參數 |
SELECT關鍵字/參數 |
說明 |
Uri |
FROM table_name |
Uri映射到提供器中命名的表 |
projection |
Col, col, col,… |
每一行中要選取的列的數組。 |
selection |
WHERE col= value |
指定選擇條件 |
selectionArgs |
用於替換selectiong參數中“?”的值。 |
|
sortOrder |
ORDER BY col, col,… |
指定行的排序標準 |