Android中讀取手機連絡人

來源:互聯網
上載者:User

標籤:pre   code   索引   不同的   更新ui   center   手機   parse   視圖   

主要就是用 內容解析者來處理問題:

首先應該先清楚Android手機連絡人的資料庫:讀取主要用了3張表:

Table Name 作用
contacts 記錄連絡人id,(有幾列即表示有幾個連絡人)
mimeTypes 儲存類型比對(為了節省資料庫的空間)
data 連絡人資訊儲存在此表中

但是在查的過程中,第一次我們是查的表contacts,第二次我們查的是view_data這個視圖

查詢過程中,第一迴圈有幾個連絡人就執行幾次,但是第二次迴圈每個連絡人最多執行11次,依據你的連絡人儲存資訊不同而不同的

一段代碼飄過:

ContentResolver contentResolver = getContentResolver();                // 用cursor物件查詢                Cursor cursor = contentResolver.query(Uri                        .parse("content://com.android.contacts/raw_contacts"),                         new String[] { "contact_id" }, null, // 查詢條件 "a=?"                        null, // 提供問號的值                        null);                //  迴圈遊標                while (cursor.moveToNext()) {                    // cursor的索引值從0開始的                    String id = cursor.getString(0);                    // 根據唯一性id值,查詢data表和mimetype產生的視圖,擷取data以及mimetype欄位                    Cursor indexCursor = contentResolver.query(                            Uri.parse("content://com.android.contacts/data"),                            new String[] { "data1", "mimetype" },                            "raw_contact_id=?", new String[] { id }, null);                    // 5 迴圈遍曆遊標的值  查詢過程還是不太清楚                    HashMap<String, String> hm = new HashMap<String, String>();                    while (indexCursor.moveToNext()) {//                        System.out.println(indexCursor.getString(0) +" "+ indexCursor.getString(1));                        hm.put(indexCursor.getString(1),indexCursor.getString(0));                    }                    contactList.add(hm);                    indexCursor.close();                }                // 關閉遊標                cursor.close();                //子線程裡發送更新ui的資料                Message msg = new Message();                msg.what = 1;                mHandler.sendMessage(msg);

 

Android中讀取手機連絡人

相關文章

聯繫我們

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