android筆記 : Content provider內容提供器

來源:互聯網
上載者:User

標籤:

內容提供器(Content Provider)主要用於在不同的應用程式之間實現資料共用的功能.

內容提供器的用法一般有兩種,一種是使用現有的內容提供器來讀取和操作相應程式中
的資料,另一種是建立自己的內容提供器給我們程式的資料提供外部存取介面。

第一種方法:使用ContentResolver來讀取和操作相應程式中的資料

1.用parse方法將內容 URI 字串解析成 Uri 對象.

uri(Uniform Resource identifier)指的是統一資源識別項
Uri uri = Uri .parse("content://com.example.databasetest.provider/book");

2.進行增刪改查的操作,如下所示:

query(查):與SqliteDatabase類似,需要藉助cursor .

Cursor cursor = getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);

if (cursor != null) {
while (cursor.moveToNext()) {
String column1 = cursor.getString(cursor.getColumnIndex("column1"));
int column2 = cursor.getInt(cursor.getColumnIndex("column2"));
}
cursor.close();
}

insert(增):執行個體化ContentValues ,調用put方法添加索引值對,然後調用 getContentResolver的insert方法.

ContentValues values = new ContentValues();
values.put("column1", "text");
values.put("column2", 1);
getContentResolver().insert(uri, values);

update(改):執行個體化ContentValues ,調用put方法添加索引值對,然後調用 getContentResolver的update方法.

ContentValues values = new ContentValues();
values.put("name", "A Storm of Swords");
values.put("pages", 1216);
values.put("price", 24.05);
getContentResolver().update(uri, values, null, null);

delete(刪):調用 ContentResolver 的 delete()方法將資料刪除

getContentResolver().delete(uri, null, null);

第二種方法:建立自己的內容提供器

a. 建立一個繼承了ContentProvider父類的類,重寫onCreate(),query(),update(),insert(),delete(),getType()方法

b. 定義一個名為CONTENT_URI,並且是public static final的Uri類型的類變數,你必須為其指定一個唯一的字串值,最好的方案是以類的全名稱, 如:
public static final Uri CONTENT_URI = Uri.parse( “content://com.google.android.MyContentProvider”);

c. 定義你要返回給用戶端的資料列名。如果你正在使用Android資料庫,必須為其定義一個叫_id的列,它用來表示每條記錄的唯一性。

 

d. 建立你的資料存放區系統。大多數Content Provider使用Android檔案系統或SQLite資料庫來保持資料,但是你也可以以任何你想要的方式來儲存。

e. 如果你要儲存位元組型資料,比如位元影像檔案等,資料列其實是一個表示實際儲存檔案的URI字串,通過它來讀取對應的檔案資料。處理這種資料類型的Content Provider需要實現一個名為_data的欄位,_data欄位列出了該檔案在Android檔案系統上的精確路徑。這個欄位不僅是供用戶端使用,而且也可以供ContentResolver使用。用戶端可以調用ContentResolver.openOutputStream()方法來處理該URI指向的檔案資源;如果是ContentResolver本身的話,由於其持有的許可權比用戶端要高,所以它能直接存取該資料檔案。

f. 聲明public static String型的變數,用於指定要從遊標處返回的資料列。

g. 查詢返回一個Cursor類型的對象。所有執行寫操作的方法如insert(), update() 以及delete()都將被監聽。我們可以通過使用ContentResover().notifyChange()方法來通知監聽器關於資料更新的資訊。

h. 在AndroidMenifest.xml中使用<provider>標籤來設定Content Provider。

i. 如果你要處理的資料類型是一種比較新的類型,你就必須先定義一個新的MIME類型,以供ContentProvider.geType(url)來返回。MIME類型有兩種形式:一種是為指定的單個記錄的,還有一種是為多條記錄的。這裡給出一種常用的格式:

 

  vnd.android.cursor.item/vnd.yourcompanyname.contenttype (單個記錄的MIME類型)
  比如, 一個請求列車資訊的URI如content://com.example.transportationprovider/trains/122 可能就會返回typevnd.android.cursor.item/vnd.example.rail這樣一個MIME類型。

 

  vnd.android.cursor.dir/vnd.yourcompanyname.contenttype (多個記錄的MIME類型)
  比如, 一個請求所有列車資訊的URI如content://com.example.transportationprovider/trains 可能就會返回vnd.android.cursor.dir/vnd.example.rail這樣一個MIME 類型。

 

參考資料:

1.《第一行代碼》內容提供器;

2.CSDN部落格:http://blog.csdn.net/chuyuqing/article/details/39995607

android筆記 : Content provider內容提供器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.