Android使用ContectResolver操作別的應用中的資料

來源:互聯網
上載者:User

        當應用程式需要與別的應用進行互動時,需要使用Android提供的兩個類,ContentProvider和ContentResolver來實現相關功能。ContentProvider用來提供內容給別的應用來操作,ContentResolver用來操作(包括查詢、插入等)別的應用資料,當然在自己的應用中也可以。 本著重講解ContentResolver的用法。

        可用通過Context來擷取ContentResolver的執行個體,在Activity中,可以直接使用下面這句:

ContentResolver resolver = this.getContentResolver();  

         擷取到該對象後,可實現如下一些常用的功能:

//查詢:   public final Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder);   //新增   public final Uri insert(Uri url, ContentValues values)       //更新   public final int update(Uri uri, ContentValues values, String where, String[] selectionArgs)   //刪除   public final int delete(Uri url, String where, String[] selectionArgs)

        查詢

query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

        描述:根據指定的URI,返回一個遊標結果集Cursor。為了提高效能,使用該方法最好能遵照以下的建議:1)對於返回結果,使用projection參數指定記錄中包含的欄位,以避免大量不用的欄位被檢索,浪費資源;2)在selection參數中使用類似phone=?的問號運算式來指定檢索條件中的欄位值,盡量不要使用指定值(比如phone='123'),這樣有利於查詢的系統緩衝。

        參數:

        uri 表示用於擷取什麼內容的URI,比如通訊錄為Phone.CONTENT_URI

        projection 欄位名稱數組,比如要擷取連絡人的顯示名稱、電話號碼、頭像ID、連絡人ID,則值為:

new String[] { Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID }

        selection 查詢條件字串,類似sql語句中的where子句,比如:display_name=? and number=?

        selectionArgs 參數selection中問號(?)對應的參數值數組

        sortOrder 對應sql語句的order by子句,如採用預設排序,值可傳null值。

        下面是讀取手機通訊錄資訊的一段代碼,首先擷取通訊錄的所有資訊,通過嵌套擷取各個連絡人的多個手機號碼:

ContentResolver contentResolver = getContentResolver();Cursor cursor = contentResolver.query (   ContactsContract.Contacts.CONTENT_URI, null, null, null, null);// 遊標移到結果集第一個行if (cursor.moveToFirst()) { // id列的索引值int idColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID); // 使用者名稱列的索引值int dispalyNameColumnIndex = cursor.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME); do {// 擷取使用者Id值String contactId = cursor.getString(idColumnIndex); //擷取使用者名稱值String displayName = cursor.getString(dispalyNameColumnIndex); // 擷取使用者號碼數量int phoneNumberCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (phoneNumberCount > 0) {Cursor phoneNumberCursor = getContentResolver().   query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, // 擷取目前使用者號碼結果集ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,null,null); //遊標移到所有號碼結果集第一個行if (phoneNumberCursor.moveToFirst()) { do {// 擷取號碼String phoneNumber = phoneNumberCursor.getString(phoneNumberCursor.getColumnIndex(   ContactsContract.CommonDataKinds.Phone.NUMBER)); } while (phoneNumberCursor.moveToNext());}// 關閉遊標phoneNumberCursor.close(); }} while(cursor.moveToNext()); }// 關閉遊標cursor.close(); 

 

       其他功能的介紹 待續......

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.