1. 程式人生 > >Android 獲取聯絡人手機號碼、姓名、地址、公司、郵箱、生日

Android 獲取聯絡人手機號碼、姓名、地址、公司、郵箱、生日

 public void testGetAllContact() throws Throwable
    {
        //獲取聯絡人資訊的Uri
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        //獲取ContentResolver
        ContentResolver contentResolver = ContactListActivity.this.getContentResolver();
        //查詢資料,返回Cursor
        Cursor cursor = contentResolver.query(uri, null
, null, null, null); List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); while(cursor.moveToNext()) { Map<String,Object> map = new HashMap<String,Object>(); StringBuilder sb = new StringBuilder();
//獲取聯絡人的ID String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); //獲取聯絡人的姓名 String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); //構造聯絡人資訊 sb.append("contactId=").append(contactId).append("
,Name=").append(name); map.put("name", name); String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));//聯絡人ID //查詢電話型別的資料操作 Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); while(phones.moveToNext()) { String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); //新增Phone的資訊 sb.append(",Phone=").append(phoneNumber); map.put("mobile", phoneNumber); } phones.close(); //查詢Email型別的資料操作 Cursor emails = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); while (emails.moveToNext()) { String emailAddress = emails.getString(emails.getColumnIndex( ContactsContract.CommonDataKinds.Email.DATA)); //新增Email的資訊 sb.append(",Email=").append(emailAddress); Log.e("emailAddress", emailAddress); map.put("email", emailAddress); } emails.close(); //Log.i("=========ddddddddddd=====", sb.toString()); //查詢==地址==型別的資料操作.StructuredPostal.TYPE_WORK Cursor address = contentResolver.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId, null, null); while (address.moveToNext()) { String workAddress = address.getString(address.getColumnIndex( ContactsContract.CommonDataKinds.StructuredPostal.DATA)); //新增Email的資訊 sb.append(",address").append(workAddress); map.put("address", workAddress); } address.close(); //Log.i("=========ddddddddddd=====", sb.toString()); //查詢==公司名字==型別的資料操作.Organization.COMPANY ContactsContract.Data.CONTENT_URI String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] orgWhereParams = new String[]{id, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; Cursor orgCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, orgWhereParams, null); if (orgCur.moveToFirst()) { //組織名 (公司名字) String company = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA)); //職位 String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)); sb.append(",company").append(company); sb.append(",title").append(title); map.put("company", company); map.put("title", title); } orgCur.close(); list.add(map); Log.i("=========orgName=====", sb.toString());//檢視所有的資料 Log.e("=========map=====", map.toString());//有很多資料的時候,只會新增一條 例如郵箱, } Log.i("=========list=====", list.toString());// cursor.close(); }
複製程式碼

許可權:

複製程式碼
    <!-- 讀聯絡人許可權 -->
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <!-- 寫聯絡人許可權 -->
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <!-- 撥號許可權 -->
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <!-- 讀簡訊許可權 -->
    <uses-permission android:name="android.permission.READ_SMS" />
複製程式碼

例子:

複製程式碼
package com.kelly.smsread;
import java.util.ArrayList;
import java.util.List;

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

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.util.Log;

/** 
 *  
 * @author larson 
 * 
 */  
public class ContactUtil {  
    public  List<Contacts> list;  
    private Context context;  
    private  JSONObject contactData;  
    private  JSONObject jsonObject;  

    public ContactUtil(Context context) {  
        this.context = context;  
    }  

    // ContactsContract.Contacts.CONTENT_URI= content://com.android.contacts/contacts;  
    // ContactsContract.Data.CONTENT_URI = content://com.android.contacts/data;  

    /** 
     * 獲取聯絡人資訊,並把資料轉換成json資料 
     *  
     * @return 
     * @throws JSONException 
     */  
    public String getContactInfo() throws JSONException {  
        list = new ArrayList<Contacts>();  
        contactData = new JSONObject();  
        String mimetype = "";  
        int oldrid = -1;  
        int contactId = -1;  
        // 1.查詢通訊錄所有聯絡人資訊,通過id排序,我們看下android聯絡人的表就知道,所有的聯絡人的資料是由RAW_CONTACT_ID來索引開的  
        // 所以,先獲取所有的人的RAW_CONTACT_ID  
        Uri uri = ContactsContract.Data.CONTENT_URI; // 聯絡人Uri;  
        Cursor cursor = context.getContentResolver().query(uri,  
                null, null, null, Data.RAW_CONTACT_ID);  
        int numm = 0;  
        while (cursor.moveToNext()) {  
            contactId = cursor.getInt(cursor  
                    .getColumnIndex(Data.RAW_CONTACT_ID));  
            if (oldrid != contactId) {  
                jsonObject = new JSONObject();  
                contactData.put("contact" + numm, jsonObject);  
                numm++;  
                oldrid = contactId;  
            }  
            mimetype = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE)); // 取得mimetype型別,擴充套件的資料都在這個型別裡面  
            // 1.1,拿到聯絡人的各種名字  
            if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) {  
                cursor.getString(cursor  
                        .getColumnIndex(StructuredName.DISPLAY_NAME));  
                String prefix = cursor.getString(cursor  
                        .getColumnIndex(StructuredName.PREFIX));  
                jsonObject.put("prefix", prefix);  
                String firstName = cursor.getString(cursor  
                        .getColumnIndex(StructuredName.FAMILY_NAME));  
                jsonObject.put("firstName", firstName);  
                String middleName = cursor.getString(cursor  
                        .getColumnIndex(StructuredName.MIDDLE_NAME));  
                jsonObject.put("middleName", middleName);  
                String lastname = cursor.getString(cursor  
                        .getColumnIndex(StructuredName.GIVEN_NAME));  
                jsonObject.put("lastname", lastname);  
                String suffix = cursor.getString(cursor  
                        .getColumnIndex(StructuredName.SUFFIX));  
                jsonObject.put("suffix", suffix);  
                String phoneticFirstName = cursor.getString(cursor  
                        .getColumnIndex(StructuredName.PHONETIC_FAMILY_NAME));  
                jsonObject.put("phoneticFirstName", phoneticFirstName);  

                String phoneticMiddleName = cursor.getString(cursor  
                        .getColumnIndex(StructuredName.PHONETIC_MIDDLE_NAME));  
                jsonObject.put("phoneticMiddleName", phoneticMiddleName);  
                String phoneticLastName = cursor.getString(cursor  
                        .getColumnIndex(StructuredName.PHONETIC_GIVEN_NAME));  
                jsonObject.put("phoneticLastName", phoneticLastName);  
            }  
            // 1.2 獲取各種電話資訊  
            if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {  
                int phoneType = cursor  
                        .getInt(cursor.getColumnIndex(Phone.TYPE)); // 手機  
                if (phoneType == Phone.TYPE_MOBILE) {  
                    String mobile = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("mobile", mobile);  
                }  
                // 住宅電話  
                if (phoneType == Phone.TYPE_HOME) {  
                    String homeNum = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("homeNum", homeNum);  
                }  
                // 單位電話  
                if (phoneType == Phone.TYPE_WORK) {  
                    String jobNum = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("jobNum", jobNum);  
                }  
                // 單位傳真  
                if (phoneType == Phone.TYPE_FAX_WORK) {  
                    String workFax = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("workFax", workFax);  
                }  
                // 住宅傳真  
                if (phoneType == Phone.TYPE_FAX_HOME) {  
                    String homeFax = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  

                    jsonObject.put("homeFax", homeFax);  
                } // 尋呼機  
                if (phoneType == Phone.TYPE_PAGER) {  
                    String pager = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("pager", pager);  
                }  
                // 回撥號碼  
                if (phoneType == Phone.TYPE_CALLBACK) {  
                    String quickNum = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("quickNum", quickNum);  
                }  
                // 公司總機  
                if (phoneType == Phone.TYPE_COMPANY_MAIN) {  
                    String jobTel = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("jobTel", jobTel);  
                }  
                // 車載電話  
                if (phoneType == Phone.TYPE_CAR) {  
                    String carNum = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("carNum", carNum);  
                } // ISDN  
                if (phoneType == Phone.TYPE_ISDN) {  
                    String isdn = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("isdn", isdn);  
                } // 總機  
                if (phoneType == Phone.TYPE_MAIN) {  
                    String tel = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("tel", tel);  
                }  
                // 無線裝置  
                if (phoneType == Phone.TYPE_RADIO) {  
                    String wirelessDev = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  

                    jsonObject.put("wirelessDev", wirelessDev);  
                } // 電報  
                if (phoneType == Phone.TYPE_TELEX) {  
                    String telegram = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("telegram", telegram);  
                }  
                // TTY_TDD  
                if (phoneType == Phone.TYPE_TTY_TDD) {  
                    String tty_tdd = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("tty_tdd", tty_tdd);  
                }  
                // 單位手機  
                if (phoneType == Phone.TYPE_WORK_MOBILE) {  
                    String jobMobile = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("jobMobile", jobMobile);  
                }  
                // 單位尋呼機  
                if (phoneType == Phone.TYPE_WORK_PAGER) {  
                    String jobPager = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("jobPager", jobPager);  
                } // 助理  
                if (phoneType == Phone.TYPE_ASSISTANT) {  
                    String assistantNum = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("assistantNum", assistantNum);  
                } // 彩信  
                if (phoneType == Phone.TYPE_MMS) {  
                    String mms = cursor.getString(cursor  
                            .getColumnIndex(Phone.NUMBER));  
                    jsonObject.put("mms", mms);  
                }  

                String mobileEmail = cursor.getString(cursor  
                        .getColumnIndex(Email.DATA));  
                jsonObject.put("mobileEmail", mobileEmail);  
            }  
        }  
        // 查詢event地址  
        if (Event.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出時間型別  
            int eventType = cursor.getInt(cursor.getColumnIndex(Event.TYPE)); // 生日  
            if (eventType == Event.TYPE_BIRTHDAY) {  
                String birthday = cursor.getString(cursor  
                        .getColumnIndex(Event.START_DATE));  
                jsonObject.put("birthday", birthday);  
            }  
            // 週年紀念日  
            if (eventType == Event.TYPE_ANNIVERSARY) {  
                String anniversary = cursor.getString(cursor  
                        .getColumnIndex(Event.START_DATE));  
                jsonObject.put("anniversary", anniversary);  
            }  
        }  
        // 獲取即時通訊訊息  
        if (Im.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出即時訊息型別  
            int protocal = cursor.getInt(cursor.getColumnIndex(Im.PROTOCOL));  
            if (Im.TYPE_CUSTOM == protocal) {  
                String workMsg = cursor.getString(cursor  
                        .getColumnIndex(Im.DATA));  
                jsonObject.put("workMsg", workMsg);  
            } else if (Im.PROTOCOL_MSN == protocal) {  
                String workMsg = cursor.getString(cursor  
                        .getColumnIndex(Im.DATA));  
                jsonObject.put("workMsg", workMsg);  
            }  
            if (Im.PROTOCOL_QQ == protocal) {  
                String instantsMsg = cursor.getString(cursor  
                        .getColumnIndex(Im.DATA));  

                jsonObject.put("instantsMsg", instantsMsg);  
            }  
        }  
        // 獲取備註資訊  
        if (Note.CONTENT_ITEM_TYPE.equals(mimetype)) {  
            String remark = cursor.getString(cursor.getColumnIndex(Note.NOTE));  
            jsonObject.put("remark", remark);  
        }  
        // 獲取暱稱資訊  
        if (Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {  
            String nickName = cursor.getString(cursor  
                    .getColumnIndex(Nickname.NAME));  
            jsonObject.put("nickName", nickName);  
        }  
        // 獲取組織資訊  
        if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出組織型別  
            int orgType = cursor.getInt(cursor  
                    .getColumnIndex(Organization.TYPE)); // 單位  
            if (orgType == Organization.TYPE_CUSTOM) { // if (orgType ==  
                // Organization.TYPE_WORK)  
                // {  
                String company = cursor.getString(cursor  
                        .getColumnIndex(Organization.COMPANY));  
                jsonObject.put(