Content Providers是Android平臺中重要的組件之一,它提供了一套標準的接口用來實現數據的增刪改查,可使各個應用程序之間實現數據共享。一方面,咱們能夠得到系統內部的如音頻、視頻、圖像和聯繫人等Content Providers,還能夠定義本身開發應用程序的CP,提供其餘應用程序訪問自身數據的接口。html
1. 對Content Providers的操做java
Content Providers把它的數據做爲數據庫模型上的單一數據表提供出來,在數據表中,每一行是一條記錄,每一列表明某一特定類型的值。下表是聯繫人信息的數據模型。android
_ID |
NUMBER |
NUMBER_KEY |
LABEL |
NAME |
TYPE |
13 |
(425) 555 6677 |
425 555 6677 |
Kirkland office |
Bully Pulpit |
TYPE_WORK |
44 |
(212) 555-1234 |
212 555 1234 |
NY apartment |
Alan Vain |
TYPE_HOME |
45 |
(212) 555-6657 |
212 555 6657 |
Downtown office |
Alan Vain |
TYPE_MOBILE |
53 |
201.555.4433 |
201 555 4433 |
Love Nest |
Rex Cars |
TYPE_HOME |
|
每個Content Providers都會會對外提供一個URI做爲其惟一標識,在與其餘程序互動的過程當中都會使用到這個常量。URI有三部分組成:數據庫
"content://"windows
數據的路徑api
ID(可選) 一個ID表明一條記錄,ID爲空表明表中的所有數據。app
在進行對Content Providers的操做中,使用到一個惟一的ContentResolver接口來使用某個具體的Content Provisers對象。其初始化代碼以下:ide
ContentResolver cr=getContentResolver();ui |
ContentResolver中定義了對Content Providers中數據操做的方法。主要包括以下幾個:spa
a. 查詢數據
方法一:使用的ContentResolver.query()方法。在SDK文檔中,有以下說明:
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) Since: API Level 1
Query the given URI, returning a Cursor over the result set.
For best performance, the caller should follow these guidelines:
-
Provide an explicit projection, to prevent reading data from storage that aren't going to be used.
-
Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
Parameters
uri |
The URI, using the content:// scheme, for the content to retrieve. |
projection |
A list of which columns to return. Passing null will return all columns, which is inefficient. |
selection |
A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI. |
selectionArgs |
You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings. |
sortOrder |
How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered. |
|
方法二:使用Activity.managedQuery()方法。須要注意的是它包含了Activity的生命週期,當Activity被暫停後,若是須要從新使用方法返回的Cursor對象,就須要經過Activity.startManagingCursor()方法從新啓動。在SDK文檔中的說明以下:
public final Cursor managedQuery (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) Since: API Level 1
Parameters
uri |
The URI of the content provider to query. |
projection |
List of columns to return. |
selection |
SQL WHERE clause. |
selectionArgs |
The arguments to selection, if any ?s are pesent |
sortOrder |
SQL ORDER BY clause. |
|
b. 添加和修改數據
ContentResolver中定義了增長和修改數據的方法,使用起來很是方便,與數據庫的操做相似。代碼以下:
-
-
- ContentValues values = new ContentValues();
- values.put(NotePad.Notes.TITLE, "title1");
- values.put(NotePad.Notes.NOTE, "NOTENOTE1");
-
- getContentResolver().insert(NotePad.Notes.CONTENT_URI, values);
-
-
-
- ContentValues values = new ContentValues();
- values.put(NotePad.Notes.TITLE, "title1");
- values.put(NotePad.Notes.NOTE, "NOTENOTE1");
- //定義加入了須要被修改記錄ID的URI
- URI uri=contentUris.withAppendedID(NotePad.Notes.CONTENT_URI,id);
-
- getContentResolver().insert(uri, values);
-
|
2. 自定義Content Provisers
能夠經過定義一個繼承於ContentProvider的類自定義本身開發應用程序的CP。經過重寫基類中的方法,能夠提供給外部操做的接口。至於在方法內部如何組織和存儲數據,則由開發者自定。例如,既可使用數據庫存儲,也可以使用XML。無論採用哪一種機制,外部用戶關注的僅僅是可操做的接口。
在實際的開發中,大多數狀況使用的是系統內的CP。因爲不多狀況自定義CP,並且模式相對較爲固定,因此咱們在使用時能夠參照現有資料或API DEMO便可。