文章出處:http://www.moandroid.com/?p=122
Android是如何實現(xiàn)應(yīng)用程序之間數(shù)據(jù)共享的?一個應(yīng)用程序可以將自己的數(shù)據(jù)完全暴露出去,外界更本看不到,也不用看到這個應(yīng)用程序暴露的數(shù)據(jù)是如何存儲的,或者是使用數(shù)據(jù)庫還是使用文件,還是通過網(wǎng)上獲得,這些一切都不重要,重要的是外界可以通過這一套標(biāo)準(zhǔn)及統(tǒng)一的接口和這個程序里的數(shù)據(jù)打交道,例如:添加(insert)、刪除(delete)、查詢(query)、修改(update),當(dāng)然需要一定的權(quán)限才可以。
如何將應(yīng)用程序的數(shù)據(jù)暴露出去? Android提供了ContentProvider,一個程序可以通過實現(xiàn)一個Content provider的抽象接口將自己的數(shù)據(jù)完全暴露出去,而且Content providers是以類似數(shù)據(jù)庫中表的方式將數(shù)據(jù)暴露。Content providers存儲和檢索數(shù)據(jù),通過它可以讓所有的應(yīng)用程序訪問到,這也是應(yīng)用程序之間唯一共享數(shù)據(jù)的方法。要想使應(yīng)用程序的數(shù)據(jù)公開化,可通過2種方法:創(chuàng)建一個屬于你自己的Content provider或者將你的數(shù)據(jù)添加到一個已經(jīng)存在的Content provider中,前提是有相同數(shù)據(jù)類型并且有寫入Content provider的權(quán)限。
如何通過一套標(biāo)準(zhǔn)及統(tǒng)一的接口獲取其他應(yīng)用程序暴露的數(shù)據(jù)?Android提供了ContentResolver,外界的程序可以通過ContentResolver接口訪問ContentProvider提供的數(shù)據(jù)。
當(dāng)前篇主要說明,如何獲取其它應(yīng)用程序共享的數(shù)據(jù),比如獲取Android 手機電話薄中的信息。
什么是URI?
在學(xué)習(xí)如何獲取ContentResolver前,有個名詞是必須了解的:URI。URI是網(wǎng)絡(luò)資源的定義,在Android中賦予其更廣闊的含義,先看個例子,如下:
將其分為A,B,C,D 4個部分:
A:標(biāo)準(zhǔn)前綴,用來說明一個Content Provider控制這些數(shù)據(jù),無法改變的;
B:URI的標(biāo)識,它定義了是哪個Content Provider提供這些數(shù)據(jù)。對于第三方應(yīng)用程序,為了保證URI標(biāo)識的唯一性,它必須是一個完整的、小寫的 類名。這個標(biāo)識在<provider> 元素的 authorities屬性中說明:
<provider name=”.TransportationProvider” authorities=”com.example.transportationprovider” . . . >
C:路徑,Content Provider使用這些路徑來確定當(dāng)前需要生什么類型的數(shù)據(jù),URI中可能不包括路徑,也可能包括多個;
D:如果URI中包含,表示需要獲取的記錄的ID;如果沒有ID,就表示返回全部;
由于URI通常比較長,而且有時候容易出錯,切難以理解。所以,在Android當(dāng)中定義了一些輔助類,并且定義了一些常量來代替這些長字符串,例如:People.CONTENT_URI
ContentResolver 介紹說明
看完這些介紹,大家一定就明白了,ContentResolver是通過URI來查詢ContentProvider中提供的數(shù)據(jù)。除了URI以外,還必須知道需要獲取的數(shù)據(jù)段的名稱,以及此數(shù)據(jù)段的數(shù)據(jù)類型。如果你需要獲取一個特定的記錄,你就必須知道當(dāng)前記錄的ID,也就是URI中D部分。
前面也提到了Content providers是以類似數(shù)據(jù)庫中表的方式將數(shù)據(jù)暴露出去,那么ContentResolver也將采用類似數(shù)據(jù)庫的操作來從Content providers中獲取數(shù)據(jù)。現(xiàn)在簡要介紹ContentResolver的主要接口,如下:
返回值 函數(shù)聲明
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.
final int update(Uri uri, ContentValues values, String where, String[] selectionArgs)Update row(s) in a content URI.
看到這里,是否感覺與數(shù)據(jù)庫的操作基本一樣的?就是這樣的,詳細解析請參考Android SQLite解析篇中的說明,不在此詳細說明。
最后一個問題:如何獲取ContentResolver?調(diào)用getContentResolver (),例如:ContentResolver cr = getContentResolver();
制作ContentResolver實例
以上就完全介紹了如何獲取、使用ContentResolver,啟動Eclipes,制作一個完整的實例如下:
打開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中<application>元素前增加如下許可:
<uses-permission android:name=”android.permission.READ_CONTACTS” />
最后運行程序,在模擬器啟動后,單擊Menu返回到Home界面,打開Contacts選擇Contacts標(biāo)簽頁,添加2個聯(lián)系人信息。返回到Home,選擇moandroid.showcontact運行,剛添加的2個聯(lián)系人信息將顯示在界面上,如下:
總結(jié)說明
ContentResolver的使用極大的方便了應(yīng)用程序之間共享數(shù)據(jù),如何將應(yīng)用程序的數(shù)據(jù)完全暴露給給他應(yīng)用程序使用了,將在下篇文章Android 應(yīng)用程序之間數(shù)據(jù)共享—-ContentProvider中說明。