Android 获取通讯录联系人

获取手机通讯录联系人的姓名,电话,地址等

Posted by Mio4kon on 2014-10-09

联系人的bean:

/**
 * 姓名
 */
private String contactName;
/**
 * 电话号码
 */
private String contactPhone;
/**
 * email
 */
private String contactEmail;
/**
 * 地址
 */
private String contactAddress;
/**
 * 机构名
 */
private String contactOrganizCompany;
/**
 * 备注
 */
private String contactRemark;

写一个帮助类用于读取联系人,并提供读取成功的回调:

/**
 * 
 * @ClassName: ContactManager
 * @Description: 联系人管理工具,用于获取联系人
 * @author mio4kon
 * @mail mio4kon.dev@gmail.com
 * @date 2014-11-7 下午4:35:49
 * 
 */
public class ContactManager {
    private Context context = BaseApplication.getApplication();
    private int count = 0;
    private List<ContactPeople> peoples;
    private JSONArray contactsJson;
    private JSONObject json;

    private OnReadContactsListener mOnReadContactsListener;
    private Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        // TODO 联系人读取完成
        Log.d("ContactManager", " 联系人读取完成!!!!");
        if (mOnReadContactsListener != null) {
            Log.d("ContactManager", " mOnReadContactsListener已设置!");
            mOnReadContactsListener.onReadContactsComplete(peoples);
            }
        };
    };

    public interface OnReadContactsListener {
        public void onReadContactsComplete(List<ContactPeople> peoples);
    }

    public void setOnReadContactsListener(OnReadContactsListener onReadContactsListener) {
        mOnReadContactsListener = onReadContactsListener;
        initContactPeople();
    }

    /** 得到联系人并处理 */
    public void initContactPeople() {
        new Thread(new ReadContactRunnable()).start();
    }


    /** 得到联系人 */
    class ReadContactRunnable implements Runnable {

        @Override
        public void run() {
            Log.d("ContactManager", " 开始读取联系人");
            peoples = new ArrayList<ContactPeople>();
            Cursor cur = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                    null, null, null, null);// 获取全部
            Log.d("ContactManager", " 联系人个数" + cur.getCount() + "");
            // 循环遍历
        if (cur.moveToFirst()) {

            int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
            int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            do {
                count++;
                Log.d("ContactManager", "处理第" + count + "联系人");
                // 联系人Bean 每读出一个联系人,就将其放入Bean中
                ContactPeople people = new ContactPeople();
                // 获得联系人的ID号
                String contactId = cur.getString(idColumn);

                // 1. 获得联系人姓名
                String disPlayName = cur.getString(displayNameColumn);
                people.setContactName(disPlayName);
                // 2. 获得联系人的电话号码
                Cursor phones = context.getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                        null, null);
                int phoneCount = phones.getCount(); // 拿到号码的个数
                Log.d("ContactManager", " 拿到号码的个数" + phoneCount + "");
                StringBuffer contactPhones = new StringBuffer();
                for (int i = 0; i < phoneCount; i++) {
                    phones.moveToNext();
                    String PhoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    if (i == phoneCount - 1) {
                        contactPhones.append(PhoneNumber);
                    } else {
                        contactPhones.append(PhoneNumber + "#");
                    }
                }
                people.setContactPhone(contactPhones.toString());
                phones.close();
                // 获取该联系人邮箱
                Cursor emails = context.getContentResolver().query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                        null, null);

                StringBuffer strbuf = new StringBuffer();
                int emailsCount = emails.getCount();
                Log.d("ContactManager", emailsCount + "");
                for (int i = 0; i < emailsCount; i++) {
                    emails.moveToNext();
                    // 遍历所有的Email
                    String emailValue = emails.getString(emails
                            .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    Log.d("ContactManager", emailValue);
                    if (i == emailsCount - 1) {
                        strbuf.append(emailValue);
                    } else {
                        strbuf.append(emailValue + "#");
                    }
                }
                people.setContactEmail(strbuf.toString());
                emails.close();
                // 获取该联系人地址
                Cursor address = context.getContentResolver().query(
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                        null, null);
                StringBuffer sbAddress = new StringBuffer();
                int addressCount = address.getCount();
                Log.d("ContactManager", "addressCount:" + addressCount);
                for (int i = 0; i < addressCount; i++) {
                    address.moveToNext();
                    // 遍历所有的联系人地址
                    // 遍历所有的地址
                    // String street = address
                    // .getString(address
                    // .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                    // String city = address
                    // .getString(address
                    // .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                    // String region = address
                    // .getString(address
                    // .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                    // String postCode = address
                    // .getString(address
                    // .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                    String formatAddress = address
                            .getString(address
                                    .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                    Log.d("ContactManager", formatAddress);
                    sbAddress.append(formatAddress + "#");
                }
                people.setContactAddress(sbAddress.toString());
                address.close();
                // 获取该联系人组织
                Cursor organizations = context.getContentResolver().query(
                        Data.CONTENT_URI,
                        new String[] { Data._ID, Organization.COMPANY, Organization.TITLE },
                        Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                                + Organization.CONTENT_ITEM_TYPE + "'",
                        new String[] { contactId }, null);
                if (organizations.moveToFirst()) {
                    do {
                        String company = organizations.getString(organizations
                                .getColumnIndex(Organization.COMPANY));
                        // String post =
                        // organizations.getString(organizations
                        // .getColumnIndex(Organization.TITLE));
                        people.setContactOrganizCompany(company);
                        // people.setcontactOrganizClass(post); //部门
                    } while (organizations.moveToNext());
                }
                organizations.close();
                // 获取备注信息
                Cursor notes = context.getContentResolver().query(
                        Data.CONTENT_URI,
                        new String[] { Data._ID, Note.NOTE },
                        Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                                + Note.CONTENT_ITEM_TYPE + "'", new String[] { contactId },
                        null);
                if (notes.moveToFirst()) {
                    do {
                        String noteinfo = notes.getString(notes.getColumnIndex(Note.NOTE));
                        people.setContactRemark(noteinfo);
                    } while (notes.moveToNext());
                }
                notes.close();
                // 获取nickname信息
                // Cursor nicknames = getContentResolver().query(
                // Data.CONTENT_URI,
                // new String[] { Data._ID, Nickname.NAME },
                // Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                // + Nickname.CONTENT_ITEM_TYPE + "'", new String[] {
                // contactId
                // },
                // null);
                // if (nicknames.moveToFirst()) {
                // do {
                // String nickname_ = nicknames.getString(nicknames
                // .getColumnIndex(Nickname.NAME));
                // people.setContactNickname(nickname_);
                // } while (nicknames.moveToNext());
                // }
                // nicknames.close();
                peoples.add(people);
            } while (cur.moveToNext());
        }
        handler.sendEmptyMessage(0);
        }
    }
    }