如何有效地获取本地电话联系人

时间:2016-03-05 16:13:45

标签: android android-contacts android-contentresolver

我使用以下代码来获取本地手机通讯录。它工作正常,并且非常快速地获取联系人。

但问题来了,在我的联系人列表中,很少有联系人有多个电子邮件和多个电话号码。

如果有多个电话或电子邮件地址,则会多次重复同一个人的姓名。 如果我改变了 ContactsContract.CommonDataKinds.Email.CONTENT_URIContactsContract.CommonDataKinds.Phone.CONTENT_URI然后它将根据联系人存在的电话号码重复名称。请帮忙

 private static final String[] PROJECTION = new String[]{
            ContactsContract.CommonDataKinds.Email.CONTACT_ID,
            ContactsContract.CommonDataKinds.Nickname.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Email.DATA,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
    };

ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null);

if (cursor != null) {
    try {
        final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
        final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
        final int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        long contactId;

        String displayName, email, phone, photo;
        while (cursor.moveToNext()) {
            mNK_UserModel = new NK_Contact();
            contactId = cursor.getLong(contactIdIndex);
            displayName = cursor.getString(displayNameIndex);

            //Adding display name
            mNK_UserModel.setFirstName(displayName);

            Util.DEBUG_LOG(1, "contact", "contact id :" + contactId);

            al_PhoneContacts.add(mNK_UserModel);
        }
    } finally {
        cursor.close();
    }
}

1 个答案:

答案 0 :(得分:0)

如果我不得不猜测我会说你错过了一次"休息"在你的while循环中。由于游标尝试获取下一个可用列条目。但看看我过去对我有用的解决方案。

它为每一行使用单独的光标,使您可以更好地控制数据。

                Map<String, String> contactDataMap = new HashMap<String, String>();
                Uri contactData = data.getData();
                Cursor cursor =  getContentResolver().query(contactData, null, null, null, null);
                cursor.moveToFirst();
                String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                contactDataMap.put(NAME, (name != null)?name:"");

                if (Integer.parseInt(cursor.getString(
                        cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id},
                            null);

                    while (pCur.moveToNext()) {
                        String number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        contactDataMap.put(PHONE, (number != null)?number:"");
                        break;
                    }
                    pCur.close();
                }

                Cursor emailCur = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);

                while (emailCur.moveToNext()) {
                    String email = emailCur.getString(
                            emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    contactDataMap.put(MAIL, (email != null)?email:"");
                    break;
                }
                emailCur.close();

                cursor.close();