安装whatsapp时获取联系人会产生重复的条目

时间:2016-02-16 08:25:05

标签: android database contacts whatsapp

我从android获取联系人并将其存储在数据库中。在模拟器上测试它,它正在工作。但是当我在手机上测试应用程序时,我得到了使用Whatsapp的联系人的重复条目。以下是代码段。

class LoadContactAsync extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        Cursor people = getContentResolver()
                .query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
                        "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
                                + ") ASC");
        while (people.moveToNext()) {
            final String contactId = people.getString(people
                    .getColumnIndex(ContactsContract.Contacts._ID));
            final String contactName = people
                    .getString(people
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            final String image_uri = people
                    .getString(people
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            final String hasPhone = people
                    .getString(people
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if ((Integer.parseInt(hasPhone) > 0)) {
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null,
                        "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
                while (phones.moveToNext()) {
                    final String phoneNumber = phones
                            .getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    db.addContact(new Contact(contactName, phoneNumber, image_uri));
                }
                phones.close();
            }
        }
        people.close();
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        pd.dismiss();
        showToast("Contacts Added Successfully");
        db.close();
    }

    @Override
    protected void onPreExecute() {
        pd = ProgressDialog.show(MainActivity.this, "Please Wait...",
                "Fetching One Time Contacts", true, false);
    }
}

以下是截图

未安装Whatsapp

With No Whatsapp installed

安装Whatsapp

With Whatsapp Installed

任何帮助将不胜感激。 感谢

3 个答案:

答案 0 :(得分:4)

如果您不想使用数据库来避免重复输入,请使用以下代码

    private HashMap<String, ContactModel> getContactsList() {
    String[] projection = new String[]{ContactsContract.Contacts._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI};
    Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");


    String lastPhoneName = " ";
    if (phones.getCount() > 0) {
        while (phones.moveToNext()) {
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String contactId = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
            String photoUri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            if (!name.equalsIgnoreCase(lastPhoneName)) {
                lastPhoneName = name;
                contactDetail.put(contactId, new ContactModel(contactId, name, phoneNumber, photoUri));
                Log.d("getContactsList", name + "---" + phoneNumber + " -- " + contactId + " -- " + photoUri);
            }
        }
    }
    phones.close();

    return contactDetail;
}

答案 1 :(得分:1)

对于可能面临同样错误的人..这对我来说很有用..在您的数据库表中将电话号码列设为unique ..

@Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT UNIQUE," + KEY_IMAGE + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

答案 2 :(得分:0)

将所有联系人数据存储在一个数组列表中(包括重复数据),

ArrayList mDetailsArrayList = new ArrayList&lt;&gt;();

然后在postExecute中使用方法removeDuplicates();

删除重复的条目
private ArrayList<Contact> removeDuplicate(ArrayList<Contact> contactArrayList){


    Set set = new TreeSet(new Comparator() {
        @Override
        public int compare(Object lhs, Object rhs) {

            if (lhs instanceof Contact && rhs instanceof Contact) {

                if (((Contact) lhs).getContactName().equalsIgnoreCase(((Contact) rhs).getContactName())) {
                    return 0;
                }
            }
            return 1;
        }
    });
    set.addAll(contactArrayList);

    System.out.println("\nAfter removing duplicates\n");

    final ArrayList newList = new ArrayList(set);

    System.out.println(set);

    return newList;
}

现在这个返回的数组列表将保存实际数据