Android 获取手机联系人信息

ContentResolver contentResolver = this.getContentResolver();

Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {

    // 获取ID
    int contactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    Log.i(TAG, "contactID=" + contactID + ",name=" + name);

    // 通过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));
        Log.i(TAG, "phoneNumber=" + phoneNumber);
    }
    phones.close();

    // 通过ID获取联系人邮箱
    Cursor emails = getContentResolver().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));
        Log.i(TAG, "emailAddress=" + emailAddress);
    }
    emails.close();
    cursor.close();
}

你可能感兴趣的:(android)