1. 程式人生 > >Android獲取通訊錄資訊

Android獲取通訊錄資訊


import android.database.Cursor;
import android.provider.ContactsContract;



import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 通訊錄工具類 - 獲取姓名,手機號碼,並轉換為JsonArray形式方便傳給伺服器
 */

public class ContactUtil {

    //    private String data;

    public static JSONArray readContacts() {
        // 首先,從raw_contacts中讀取聯絡人的id("contact_id")
        // 其次,根據contact_id從data表中查詢出相應的電話號碼和聯絡人名稱
        // 然後,根據mimetype來區分哪個是聯絡人,哪個是電話號碼
        // Uri rawContactsUri = Uri
        // .parse("content://com.android.contacts/raw_contacts");

        ArrayList<HashMap<String, String>> contacts = new ArrayList<HashMap<String, String>>();
        JSONArray jsonArray = new JSONArray();
        JSONObject tmpObj = new JSONObject();

        // 從raw_contacts中讀取聯絡人的id("contact_id")
        Cursor rawContactsCursor = MyApplication.getContext().getContentResolver().query(
                ContactsContract.RawContacts.CONTENT_URI,
                new String[]{"contact_id", "sort_key"}, null, null,
                "sort_key");    //按sort_key排序

        if (rawContactsCursor != null) {
            while (rawContactsCursor.moveToNext()) {
                String contactId = rawContactsCursor.getString(0);
                // System.out.println(contactId);
                if (contactId != null) {
                    // 曾經有過,已經刪除的聯絡人在raw_contacts表中記錄仍在,但contact_id值為null
                    // 根據contact_id從data表中查詢出相應的電話號碼和聯絡人名稱
                    // Uri dataUri = Uri
                    // .parse("content://com.android.contacts/data");
                    Cursor dataCursor = MyApplication.getContext().getContentResolver().query(
                            ContactsContract.Data.CONTENT_URI,
                            new String[]{"data1", "mimetype"},
                            "contact_id=?", new String[]{contactId}, null);

                    if (dataCursor != null) {
                        HashMap<String, String> map = new HashMap<String, String>();

                        while (dataCursor.moveToNext()) {
                            String data1 = dataCursor.getString(0);
                            String mimetype = dataCursor.getString(1);

                            if (data1 == null) {
                                data1 = "";
                            }

                            tmpObj = new JSONObject();
                            if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
                                map.put("iphone", data1);
                            } else if ("vnd.android.cursor.item/name".equals(mimetype)) {
                                //                                map.put("name", data1);
                                map.put("displayName", data1);
                                //                            } else if ("vnd.android.cursor.item/group_membership".equals(mimetype)) {
                                //                                map.put("tagLabel", data1);
                                //                            }else{
                                //                                LogUtil.e("mimetype ------------- ", mimetype + "   " + data1);
                            }
                        }
                        try {
                            tmpObj.put("iphone", map.get("iphone") == null ? "" : map.get("iphone"));
                            tmpObj.put("tagLabel", map.get("tagLabel") == null ? "" : map.get("tagLabel"));
                            tmpObj.put("displayName", map.get("displayName") == null ? "" : map.get("displayName"));
                            jsonArray.put(tmpObj);
                            contacts.add(map);
                        } catch (JSONException e) {
                            LogUtil.e("e ----------------- ", e.getMessage());
                            e.printStackTrace();
                        }
                    }
                    dataCursor.close();
                }
            }

            LogUtil.e("ContactUtil", "rawContactsCursor --- " + contacts);
            LogUtil.e("ContactUtil", "jsonArray --- " + jsonArray.toString());
        }
        return jsonArray;
    }
}