Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 讀取手機全部聯絡人資訊的完整範例

編輯:關於 Android 程式設計

想在自己的應用裡列出通訊錄,本質上是透過 ContentResolver 查詢系統的 Contacts 內容提供者。整個流程分兩步:先定義列表項的樣式,再撰寫查詢邏輯。

一、列表項佈局

新增 contact_item.xml,左側放姓名,右側放電話號碼並搭配一個撥號圖示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="10dip"
        android:text="姓名:"
        android:textColor="#000000"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="80dp"
        android:layout_marginTop="10dip"
        android:drawableLeft="@android:drawable/ic_menu_call"
        android:text="電話號"
        android:textColor="#000000"
        android:textSize="20dp" />
</RelativeLayout>

二、查詢邏輯

關鍵觀念是:聯絡人資料被拆成兩層。raw_contacts 提供 contact_id 清單,而真正的姓名、電話等欄位則存在 data 表中,需要再用 contact_id 二次查詢,並依 mimetype 判斷這筆資料究竟是哪一類。

private List<Map<String, String>> getContactInfo() {
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();

    ContentResolver resolver = getContentResolver();
    Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
    Uri uriData = Uri.parse("content://com.android.contacts/data");

    Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null);
    while (cursor.moveToNext()) {
        String contactId = cursor.getString(0);
        if (contactId == null) {
            continue;
        }
        Map<String, String> map = new HashMap<String, String>();
        Cursor dataCursor = resolver.query(uriData,
                new String[] { "data1", "mimetype" },
                "contact_id=?", new String[] { contactId }, null);
        while (dataCursor.moveToNext()) {
            String data1 = dataCursor.getString(0);
            String mimetype = dataCursor.getString(1);
            if ("vnd.android.cursor.item/name".equals(mimetype)) {
                map.put("name", data1);
            } else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
                map.put("phone", data1);
            }
        }
        list.add(map);
        dataCursor.close();
    }
    cursor.close();
    return list;
}

三、綁定到 ListView

list_select_contact = (ListView) findViewById(R.id.list_select_contact);
List<Map<String, String>> data = getContactInfo();
list_select_contact.setAdapter(new SimpleAdapter(this, data,
        R.layout.contact_item,
        new String[] { "name", "phone" },
        new int[] { R.id.tv_name, R.id.tv_phone }));

四、兩個實務提醒

權限不能少。需在 AndroidManifest.xml 中聲明 android.permission.READ_CONTACTS;Android 6.0 以上還必須在執行期動態申請,否則 query 會直接丟出 SecurityException。

API 已過時。這裡使用的 content://com.android.contacts/ 在 API 5 之後已被 ContactsContract 取代。新專案建議改用 ContactsContract.Contacts 搭配 ContactsContract.CommonDataKinds.Phone,並以 Data.CONTACT_ID 做關聯查詢,能少掉一層巢狀迴圈。

另外別忘了:Cursor 一定要關閉,否則在大量資料下很容易觸發 cursor leak 警告。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved