成人在线一区二区,91精品亚洲,久久久久97http://www.aygfsteel.com/fridayhaha/zh-cnFri, 20 Jun 2025 01:03:58 GMTFri, 20 Jun 2025 01:03:58 GMT60創(chuàng)建Content Providershttp://www.aygfsteel.com/fridayhaha/archive/2009/07/20/287468.htmldgshinedgshineMon, 20 Jul 2009 06:38:00 GMThttp://www.aygfsteel.com/fridayhaha/archive/2009/07/20/287468.htmlhttp://www.aygfsteel.com/fridayhaha/comments/287468.htmlhttp://www.aygfsteel.com/fridayhaha/archive/2009/07/20/287468.html#Feedback0http://www.aygfsteel.com/fridayhaha/comments/commentRss/287468.htmlhttp://www.aygfsteel.com/fridayhaha/services/trackbacks/287468.html

創(chuàng)建Content Providers

2009-07-03 IMTI Haerbin dg

目標(biāo):

       創(chuàng)建Content Providers ,讓自己的程序所收集的數(shù)據(jù)能夠被其他應(yīng)用所訪問

應(yīng)用案例:

       Service Http形式抓取網(wǎng)絡(luò)地震信息,存入本地SQLite3,供本地應(yīng)用查詢,統(tǒng)計(jì),同時(shí)以Content Providers形式對(duì)外提供數(shù)據(jù),讓另一個(gè)基于google地圖的程序能夠根據(jù)經(jīng)緯度在地圖上顯示地震區(qū)域,并且可以向Content Providers發(fā)起查詢得到經(jīng)緯度

問題:

1, 怎么創(chuàng)建Content Providers

2, 我們的程序怎么經(jīng)由Content Providers訪問數(shù)據(jù)

別人的程序怎么經(jīng)由Content Providers查詢我們的數(shù)據(jù)

關(guān)鍵角色:

1, Content URI

2, SQLiteOpenHelper

3, ContentResolver

4, SQLiteQueryBuilder

5, Content Providers工作原理

介紹 

       Content Providers 是一種讓我們能夠在不同應(yīng)用間共享數(shù)據(jù) 的唯一的 通用化接口機(jī)制,通過對(duì)底層的數(shù)據(jù)源進(jìn)行抽象,Content Providers 解除了應(yīng)用程序?qū)雍蛿?shù)據(jù)層的耦合,這樣應(yīng)用程序可以輕便的在不同數(shù)據(jù)源之上切換(和DAO的理想相當(dāng)一致)

       Content Providers 使用 權(quán)限控制,通過URI模式訪問,讀寫雙向,由此,任何具有對(duì)應(yīng)permissions(許可、權(quán)限)的應(yīng)用程序都可以增刪查改 由另一個(gè)程序創(chuàng)建的數(shù)據(jù)---包括一些本地化Android數(shù)據(jù)庫(kù)(也就是說你的程序可以Happy的訪問通訊錄,瀏覽器歷史等

       同樣,你可以把你自己的數(shù)據(jù)源(你開發(fā)的飯館評(píng)價(jià)系統(tǒng)的數(shù)據(jù))發(fā)布成Content Providers ,這樣其他開發(fā)者就可以和你所開發(fā)的應(yīng)用中的數(shù)據(jù)進(jìn)行交互,甚至擴(kuò)展(別人可以在你的數(shù)據(jù)基礎(chǔ)上挖掘開發(fā)出菜品評(píng)價(jià)系統(tǒng))

倒敘進(jìn)行:如何調(diào)用Content Providers

       我們可以使用ContentResoler訪問Content Providers,每一個(gè)應(yīng)用上下文android.content.Context(Activity是他的子類)都持有一個(gè)ContentResolver引用,于是

      

ContentResolver cr = getContentResolver();就很貼心的出現(xiàn)了

 

ContentResolver我們拿到手后,可以使用query(),insert(),update()等方法,但是還有一個(gè)URI

       備注:content URI             


A.       標(biāo)準(zhǔn)的前綴,不可以修改

B.       標(biāo)識(shí)部分,對(duì)于第三方應(yīng)用(用戶自己開發(fā)的應(yīng)用),此處應(yīng)該使用完整的包名類名來(lái)保證唯一性,其實(shí)是對(duì)應(yīng)在manifest.xml<provider>節(jié)點(diǎn)的authorities屬性值<provider class="TransportationProvider" authorities="com.example.transportationprovider" />

C.       content providers用來(lái)確定是何種數(shù)據(jù)請(qǐng)求的,這個(gè)可以沒有,也可以有多個(gè),如果content provider僅提供一種類型的數(shù)據(jù)(只有trains,例如咱們這個(gè)例子),那么此段可以省略。如果可以提供幾種類型的數(shù)據(jù),那么就可能形如: "land/bus, land/train, sea/ship, and sea/submarine"

D.       被指定的記錄,一般是被請(qǐng)求記錄的id值,如果被申請(qǐng)使用指定類型的所有數(shù)據(jù),那么形如content://com.example.transportationprovider/trains

需要關(guān)心,作為ContentResolver,需要使用URI來(lái)告訴Android 想要和哪一個(gè)ContentProvider交互(調(diào)用)


1 //返回所有的地震信息
2 
3     Cursor c = cr.query(EarthquakeProvider.CONTENT_URI, PROJECTION, null,         null, EarthquakeProvider.DEFAULT_SORT_ORDER);

其中


1 public staticfinal Uri CONTENT_URI = Uri
2 
3            .parse("content://com.paad.provider.Earthquake/earthquakes");
4 
5 Content Provider 的URI 是在AndroidManifest.xml 中聲明的,如下:
6 
7 <!-- 注冊(cè)ContentProvider -->
8 
9        <provider android:name=".earthquake.EarthquakeProvider" android:authorities="com.paad.provider.Earthquake" />

可見,authorities是一個(gè)標(biāo)識(shí),是我們自定義的,大多Content Providers都定義一個(gè)CONTENT_URI屬性值存儲(chǔ)對(duì)應(yīng)xml中聲明的authorities ,這樣方便我們隨時(shí)取用

 

通常 Content Providers 展現(xiàn)兩種形式的URI入口,一個(gè)對(duì)應(yīng)查詢所有的請(qǐng)求,另一個(gè)對(duì)應(yīng)查詢單條記錄

查詢所有記錄的URI:

    content://com.paad.provider.Earthquake/earthquakes

查詢單條記錄的URI:

    content://com.paad.provider.Earthquake/earthquakes/#

創(chuàng)建Content Providers 步驟

繼承ContentProvider

    重寫onCreateupdatequeryinsertdeletegetType回調(diào)方法,當(dāng)應(yīng)用程序用ContentResolver.insert()等操作時(shí),此處的函數(shù)被調(diào)用

定義public static final Uri  CONTENT_URI屬性,此變量代表你的ContentProvider能夠處理的 URI ,必須是唯一的,(實(shí)際是引用AndroidManifest.xml中的定義),

   

1 public staticfinal Uri CONTENT_URI = Uri
2 
3        .parse("content://com.paad.provider.Earthquake/earthquakes");

構(gòu)建數(shù)據(jù)存儲(chǔ)系統(tǒng),我們可以使用文件存儲(chǔ)或是SQLite數(shù)據(jù)庫(kù),或其他

    針對(duì)SQLite3,我們的步驟如下:

l         定義內(nèi)部類earthquakeDatabaseHelper繼承SQLiteOpenHelper,并重寫onCreate(SQLiteDatabase db)     onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)方法   earthquakeDatabaseHelper的作用是封裝了創(chuàng)建數(shù)據(jù)庫(kù)、更新數(shù)據(jù)庫(kù)版本的操作

l         對(duì)于query(),我們?cè)谄鋬?nèi)部依賴Cursor SQLiteQueryBuilder.query()發(fā)送查詢,對(duì)于其他insert,update,delete我們直接借助SQLiteDatabase.insert等完成

l         Content ProvideronCreate()中,我們做如下初始化

       

1 Context context = getContext();
2 
3        earthquakeDatabaseHelper dbHelper;
4 
5        dbHelper = new earthquakeDatabaseHelper(context, DATABASE_NAME,        null,DATABASE_VERSION);
6 
7        earthquakeDB = dbHelper.getWritableDatabase();
8 
9        return (earthquakeDB == null? false : true;

定義我們的查詢時(shí)所會(huì)用到的列名,一般我們會(huì)把數(shù)據(jù)庫(kù)中表的所有列名都定義成常量,方便查詢時(shí)Cursor提取值

   

// Column Names 表中列名
 
     
public static final String KEY_ID = "_id";
 
     
public static final String KEY_DATE = "date";
 
     
public static final String KEY_DETAILS = "details";
 
     
public static final String KEY_LOCATION_LAT = "latitude";//……

URI 數(shù)據(jù)請(qǐng)求來(lái)了,作為Content Provider的開發(fā)者你需要告訴Conent Provider 怎么判斷URI想要哪種數(shù)據(jù),這里需借助UriMatcher

   

private static final UriMatcher uriMatcher;

    
// Allocate the UriMatcher object, where a URI ending in ‘earthquakes’

    
// will correspond to a request for all earthquakes, and ‘earthquakes’

    
// with a trailing ‘/[rowID]’ will represent a single earthquake row.

    
static {

       uriMatcher 
= new UriMatcher(UriMatcher.NO_MATCH);

       
// 匹配查詢所有com.paad.provider.Earthquake

       uriMatcher.addURI(
"com.paad.provider.Earthquake""earthquakes", QUAKES);

       
// 匹配查詢單個(gè)

       uriMatcher.addURI(
"com.paad.provider.Earthquake""earthquakes/#",QUAKE_ID);

    }

這個(gè)變量Content Provider getType()中會(huì)使用進(jìn)行URI匹配



]]>
主站蜘蛛池模板: 星子县| 定襄县| 泸溪县| 白朗县| 浏阳市| 大港区| 莫力| 榆树市| 浠水县| 勐海县| 搜索| 抚顺市| 无棣县| 临邑县| 化州市| 宁陵县| 武穴市| 贵溪市| 宁德市| 即墨市| 连云港市| 德清县| 益阳市| 乐陵市| 四子王旗| 隆回县| 民县| 辽阳县| 华蓥市| 泰来县| 壤塘县| 北宁市| 共和县| 余姚市| 天等县| 定结县| 和林格尔县| 边坝县| 饶阳县| 讷河市| 呼玛县|