天天看點

【轉】Android 應用程式之間資料共享—ContentResolver

Android是如何實作應用程式之間資料共享的?一個應用程式可以将自己的資料完全暴露出去,外界更本看不到,也不用看到這個應用程式暴露的資料是如何存儲的,或者是使用資料庫還是使用檔案,還是通過網上獲得,這些一切都不重要,重要的是外界可以通過這一套标準及統一的接口和這個程式裡的資料打交道,例如:添加(insert)、删除(delete)、查詢(query)、修改(update),當然需要一定的權限才可以。

如何将應用程式的資料暴露出去? Android提供了ContentProvider,一個程式可以通過實作一個Content provider的抽象接口将自己的資料完全暴露出去,而且Content providers是以類似資料庫中表的方式将資料暴露。Content providers存儲和檢索資料,通過它可以讓所有的應用程式通路到,這也是應用程式之間唯一共享資料的方法。要想使應用程式的資料公開化,可通過2種方法:建立一個屬于你自己的Content provider或者将你的資料添加到一個已經存在的Content provider中,前提是有相同資料類型并且有寫入Content provider的權限。

如何通過一套标準及統一的接口擷取其他應用程式暴露的資料?Android提供了ContentResolver,外界的程式可以通過ContentResolver接口通路ContentProvider提供的資料。

目前篇主要說明,如何擷取其它應用程式共享的資料,比如擷取Android 手機電話薄中的資訊。

在學習如何擷取ContentResolver前,有個名詞是必須了解的:URI。URI是網絡資源的定義,在Android中賦予其更廣闊的含義,先看個例子,如下:

<a href="http://www.moandroid.com/wp-content/uploads/2009/08/URI.JPG"></a>

将其分為A,B,C,D 4個部分:

A:标準字首,用來說明一個Content Provider控制這些資料,無法改變的;

B:URI的辨別,它定義了是哪個Content Provider提供這些資料。對于第三方應用程式,為了保證URI辨別的唯一性,它必須是一個完整的、小寫的 類名。這個辨別在&lt;provider&gt; 元素的 authorities屬性中說明:

&lt;provider name=”.TransportationProvider” authorities=”com.example.transportationprovider” . . . &gt;

C:路徑,Content Provider使用這些路徑來确定目前需要生什麼類型的資料,URI中可能不包括路徑,也可能包括多個;

D:如果URI中包含,表示需要擷取的記錄的ID;如果沒有ID,就表示傳回全部;

由于URI通常比較長,而且有時候容易出錯,切難以了解。是以,在Android當中定義了一些輔助類,并且定義了一些常量來代替這些長字元串,例如:People.CONTENT_URI

看完這些介紹,大家一定就明白了,ContentResolver是通過URI來查詢ContentProvider中提供的資料。除了URI以外,還必須知道需要擷取的資料段的名稱,以及此資料段的資料類型。如果你需要擷取一個特定的記錄,你就必須知道目前記錄的ID,也就是URI中D部分。

前面也提到了Content providers是以類似資料庫中表的方式将資料暴露出去,那麼ContentResolver也将采用類似資料庫的操作來從Content providers中擷取資料。現在簡要介紹ContentResolver的主要接口,如下:

傳回值

函數聲明

final Uri

insert(Uri url, ContentValues values)Inserts a row into a table at the given URL.

final int

delete(Uri url, String where, String[] selectionArgs)Deletes row(s) specified by a content URI.

final Cursor

query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)Query the given URI, returning a Cursor over the result set.

update(Uri uri, ContentValues values, String where, String[] selectionArgs)Update row(s) in a content URI.

最後一個問題:如何擷取ContentResolver?調用getContentResolver (),例如:ContentResolver cr = getContentResolver();

以上就完全介紹了如何擷取、使用ContentResolver,啟動Eclipes,制作一個完整的執行個體如下:

<a href="http://www.moandroid.com/wp-content/uploads/2009/08/ContentResolver.JPG"></a>

打開showcontent.java,修改如下:

package moandroid.showcontact;

import android.app.ListActivity;

import android.database.Cursor;

import android.os.Bundle;

import android.provider.Contacts.Phones;

import android.widget.ListAdapter;

import android.widget.SimpleCursorAdapter;

public class showcontact extends ListActivity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);

startManagingCursor(c);

ListAdapter adapter = new SimpleCursorAdapter(this,

android.R.layout.simple_list_item_2, c,

new String[] { Phones.NAME, Phones.NUMBER },

new int[] { android.R.id.text1, android.R.id.text2 });

setListAdapter(adapter);

}

然後在AndroidManifest.XML中&lt;application&gt;元素前增加如下許可:

&lt;uses-permission android:name=”android.permission.READ_CONTACTS” /&gt;

最後運作程式,在模拟器啟動後,單擊Menu傳回到Home界面,打開Contacts選擇Contacts标簽頁,添加2個聯系人資訊。傳回到Home,選擇moandroid.showcontact運作,剛添加的2個聯系人資訊将顯示在界面上,如下:

<a href="http://www.moandroid.com/wp-content/uploads/2009/08/Content.JPG"></a>

ContentResolver的使用極大的友善了應用程式之間共享資料,如何将應用程式的資料完全暴露給給他應用程式使用了,将在下篇文章Android 應用程式之間資料共享—-ContentProvider中說明。