Android聯(lián)系人模塊分析(一)_第1頁
Android聯(lián)系人模塊分析(一)_第2頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

Android聯(lián)系人模塊分析(一)聯(lián)系人模塊是Android系統(tǒng)中的一個重要的功能模塊,目前Android市場上有大量的基于通訊錄的應(yīng)用,本人計劃寫出系列文章詳細介紹聯(lián)系人模塊的結(jié)構(gòu)和使用。作為開頭,本篇文章將對ContactsProvider做個簡要的介紹。Android系統(tǒng)將聯(lián)系人的信息存儲在sqlite數(shù)據(jù)庫中,并通過ContactsProvider提供查詢、更新、刪除等操作。所以如果需要掌握聯(lián)系人相關(guān)信息的操作,就需要掌握ContactsProvider的內(nèi)容。在開始學(xué)習ContactsProvider的內(nèi)容之前,讀者需要對ContentProvider機制有較深入的理解,ContactsProvider本質(zhì)上就是Android內(nèi)建的一種ContentProvider。ContentProvider是Android系統(tǒng)用來在所有應(yīng)用程序之間共享數(shù)據(jù)的一種方法。下面我就從數(shù)據(jù)訪問和存儲的角度來介紹如何自定義一個ContentProvider,數(shù)據(jù)存儲在sqlite中:1、提供外部訪問的接口:訪問接口是數(shù)據(jù)操作的前提,用戶不需要知道內(nèi)部數(shù)據(jù)的存儲方式,只要通過數(shù)據(jù)訪問接口就可以獲取Provider中的數(shù)據(jù)信息,以下為ContentProvider.java中定義的數(shù)據(jù)訪問、操作的接口:[java]viewplaincopypublicabstractCursorquery(Uriuri,String口projection,Stringselection,String[]selectionArgs,StringsortOrder);publicabstractUriinsert(Uriuri,ContentValuesvalues);publicabstractintupdate(Uriuri,ContentValuesvalues,Stringselection,String口selectionArgs);實現(xiàn)自定義ContentProvider,可以直接或間接的派生ContentProvider類,并override以上幾種數(shù)據(jù)操作方法。以ContactsProvider為例,在ContactsProvider2.java文件中,我們可以看到:[java]viewplaincopypublicclassContactsProvider2extendsAbstractContactsProviderimplementsOnAccountsUpdateListenerpublicabstractclassAbstractContactsProviderextendsContentProviderimplementsSQLiteTransactionListenerContactsProvider派生自AbstractContactsProvider,而AbstractContactsProvider又從ContentProvider派生,從AbstractContactsProvider.java中我們就可以看到其override了一些數(shù)據(jù)操作方法。2、創(chuàng)建數(shù)據(jù)庫:通過派生SQLiteOpenHelper類,創(chuàng)建數(shù)據(jù)庫及其各個表項信息;在ContactsProvider中對應(yīng)的即為ContactsDatabaseHelper類:[java]viewplaincopypublicclassContactsDatabaseHelperextendsSQLiteOpenHelper為了讓所有數(shù)據(jù)庫訪問者保持一致性,該類設(shè)計為單例類,該單例在ContactsProvider創(chuàng)建時創(chuàng)建:[java]viewplaincopypublicbooleanonCreate(){mDbHelper=newContactsSearchDBHelper(getContextO);returntrue;}通過該數(shù)據(jù)庫幫助類,在訪問數(shù)據(jù)庫時,可以通過該幫助類獲得數(shù)據(jù)庫的讀、寫引用,如下面的insert操作:[java]viewplaincopypublicUriinsert(Uriuri,ContentValuesvalues){<strong>SQLiteDatabasedbmDbHelper.getWritableDatabase();</strong>intmatch=sUriMatcher.match(uri);longid=-1;17synchronized(mLock){db.beginTransaction();try{switch(match){/*caseCONTACT_INFO:id<strong>db</strong>.insert(ContactInfo.TABLE_NAME,"",values);db.setTransactionSuccessful();break;*/caseCircleMemberDATA:id<strong>db</strong>.insert(CircleMember.TABLE_NAME,"",values);db.setTransactionSuccessful();break;3、定義數(shù)據(jù)表名、字段名,及其URI:在ContactsProvider中,ContactsContract類定義了該provider的權(quán)限、數(shù)據(jù)表、URI信息:[java]viewplaincopy/**Theauthorityforthecontactsprovider*/publicstaticfinalStringAUTHORITY="com.android.contacts";/**Acontent://styleuritotheauthorityforthecontactsprovider*/publicstaticfinalUriAUTHORITY_URI=Uri.parse("content://"+AUTHORITY);Contacts數(shù)據(jù)表的定義:[java]viewplaincopypublicstaticclassContactsimplementsBaseColumns,ContactsColumns,ContactOptionsColumns,ContactNameColumns,ContactStatusColumns{TOC\o"1-5"\h\z/***Thisutilityclasscannotbeinstantiated*/privateContacts(){}40/***Thecontent://styleURIforthistable*/publicstaticfinalUriCONTENT_URI=Uri.withAppendedPath(AUTHORITY_URI,""contacts〃);上述只是簡單的介紹了ContactsProvider的結(jié)構(gòu),下面我們來詳細介紹一下ContactsProvider中的代碼:首先看看URI匹配表的生成邏輯:[java]viewplaincopystatic{//ContactsURImatchingtablefinalUriMatchermatcher=sUriMatcher;matcher.addURI(ContactsContract.AUTHORITY,""contacts〃,CONTACTS);matcher.addURI(ContactsContract.AUTHORITY,"contacts/#〃,CONTACTS_ID);matcher.addURI(ContactsContract.AUTHORITY,"contacts/#/data〃,CONTACTS_ID_DATA);matcher.addURI(ContactsContract.AUTHORITY,"contacts/#/entities〃,CONTACTS_ID_ENTITIES);matcher.addURI(ContactsContract.AUTHORITY,"contacts/#/suggestions〃,AGGREGATION_SUGGESTIONS);從URI匹配表的生成邏輯可以看出該Provider提供了哪些查詢的方法,以這一行代碼為例:matcher.addURI(ContactsContract.AUTHORITY,"contacts/#/data〃,CONTACTS_ID_DATA);這個URI就是對Contact表和Data表進行關(guān)聯(lián)查詢,當然這個查詢是需要帶上contactId的,目前ContactsProvider不支持其他字段的Contact表和Data表關(guān)聯(lián)查詢。用戶就可以根據(jù)該URI來進行如下的查詢操作:[java]viewplaincopyUricontactUri=ContentUris.withAppendedId(Contacts.CONTENT_URI,contactId);StringdataDir=ContactsContract.Contacts.Data.CONTENT_DIRECTORY;56UriMethodUri=Uri.withAppendedPath(contactUri,dataDir);575859606162636465Cursorc=null;try信息,c=mContext.getContentResolver().query(methodUri,null,null,null,null);catch(IllegalArgumentExceptione){if(c!=null){c.close();}上述查詢代碼就是通過contactId

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論