从手机中获取不同且独特的联系人,避免欺骗性

时间:2016-09-22 11:59:49

标签: android contactscontract

我想从手机中检索联系人列表,避免重复。我认为它是从谷歌帐户,手机或SIM卡检索联系人。所以如何避免重复的联系人。虽然联系人可以有相同的号码,但号码不同。但同名和相同的号码不应出现在列表中。 这是代码。

ContentResolver cr = getContentResolver();
        final String[] PROJECTION = new String[] {
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER
        };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
                + ("1") + "'";
        //String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, selection
                + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
                + "=1", null,  ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

        if(cur != null) {
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {

                    String contactPhone = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String contactName = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String contactId = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                    contacts.add(new Contact().setId(contactId).setName(contactName).setNumber(contactPhone));
                }
            }

            cur.close();
        }

2 个答案:

答案 0 :(得分:1)

我不确定它是否可靠,但它对我有用

首先将ArrayMap<String, String> arrayMap = new ArrayMap<>(); // // while (cursor.moveToNext()){ String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); arrayMap.put(phone, name); } // // for(String key: arrayMap.keySet()){ JSONObject jsonObject = new JSONObject(); try { jsonObject.put("name", arrayMap.get(key)); jsonObject.put("phone_number", key); contactsList.put(jsonObject); }catch(JSONException e){ e.printStackTrace(); } } 中的联系人复制为电话号码作为键,联系人姓名作为值。在这种方法中,即使重复电话号码,它也只会更新名称。

创建ArrayMap后,您可以使用迭代器检索具有唯一电话号码的联系人。

new Contacts(...)

在您的情况下,您可以直接调用row_number()而不是使用JSONObject。

答案 1 :(得分:1)

独特的联系人获取和数据加载快速此解决方案更好

String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
String order = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
String[] projection = new String[] {
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
        ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.Contacts.LOOKUP_KEY
};

void getAllContacts() {
    Cursor crContacts = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, selection, null, order);
    String temp_name="";
    while (crContacts.moveToNext()) {
        FetchFamilymember temp = new FetchFamilymember();
        String name = crContacts.getString(crContacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phone = crContacts.getString(crContacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        String photoUri = crContacts.getString(crContacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

        if (name.equals(temp_name))
            continue;
        temp_name=name;
        temp.setName(name);
        temp.setPhone(phone);
        temp.setIsselect(false);
        familylist.add(temp);
        Log.d("readContacts: " , name + " Contactnumber" + phone);

    }
    crContacts.close();

}