【问题标题】:How do I delete duplicate contacts programmatically?如何以编程方式删除重复的联系人?
【发布时间】:2019-04-11 07:26:41
【问题描述】:

我有一个应用程序,可以从我的 Web 服务器上的 XML 文件将联系人插入手机。该应用程序每天重新插入一次联系人,因此我有最新的联系人和号码等。但是,我想从第一次插入中删除所有联系人,以免重复。

在插入列表之前,我尝试过的所有代码都会导致所有联系人被删除。因此,这也会删除不属于 XML 列表的其他联系人。

for (int temp = 0; temp < mainList.getLength(); temp++) {

Node mainNode = mainList.item(temp);
Element eElement = (Element) mainNode;
list.add(getChildElementContent(eElement, "NAME") + ": " + 
getChildElementContent(eElement, "NUMBER"));
System.out.println(list.get(temp));

ArrayList<ContentProviderOperation> ops = new 
ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
    null, null, null, null);
while (cur.moveToNext()) {
    try{
        String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
        System.out.println("The uri is " + uri.toString());
        cr.delete(uri, ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + "=" + getChildElementContent(eElement,"NAME"), null);
        System.out.println(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)));
        System.out.println( getChildElementContent(eElement,"NAME"));
    }
    catch(Exception e)
    {
        System.out.println(e.getStackTrace());
    }
}

where 子句应该只删除与我的 XML 文件中的联系人同名的联系人。但它会删除手机上的所有联系人。

【问题讨论】:

  • 与其删除并重新上传ijng,不如更新现有的联系人。删除和重新创建可能会搞砸各种其他应用程序。

标签: java android android-contacts


【解决方案1】:
try this :

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
    String lastnumber = "0";

    if (cur.getCount() > 0)
    {
        while (cur.moveToNext())
        {
            String number = null;
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]
                { id }, null);
                while (pCur.moveToNext())
                {
                    number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.e("lastnumber ", lastnumber);
                    Log.e("number", number);

                    if (number.equals(lastnumber))
                    {

                    }
                    else
                    {
                        lastnumber = number;

                        Log.e("lastnumber ", lastnumber);
                        int type = pCur.getInt(pCur.getColumnIndex(Phone.TYPE));
                        switch (type)
                        {
                            case Phone.TYPE_HOME:
                                Log.e("Not Inserted", "Not inserted");
                                break;
                            case Phone.TYPE_MOBILE:

                                databaseHandler.insertContact(id, name, lastnumber, 0);
                                break;
                            case Phone.TYPE_WORK:
                                Log.e("Not Inserted", "Not inserted");
                                break;
                        }

                    }

                }
                pCur.close();
            }

        }
    }

我先在 sqlite 数据库中插入数据,然后按名称编写选择查询。

【讨论】:

  • 这实际上并没有删除任何东西?从 XML 文件重新插入时,我需要从列表中删除重复项。
【解决方案2】:

我非常同意这个评论,你应该更新你的联系人,而不是全部删除然后重新插入。

但是,如果您仍要这样做,则需要确保将应用的所有联系人都插入到应用自己的 ACCOUNT_NAME/ACCOUNT_TYPE 下。 然后,您可以轻松删除仅来自您的应用的RawContacts。

您的代码的问题是您要删除整个联系人,而应该只删除 RawContacts。

在此处查看您应该如何插入应用的 RawContacts:https://developer.android.com/reference/android/provider/ContactsContract.RawContacts#operations 您需要为ACCOUNT_TYPE 找到一个唯一的字符串,这样就不会与其他应用发生冲突。

删除所有应用的RawContacts 然后很容易通过:

String selection = RawContacts.ACCOUNT_TYPE + "=" + myAccountType;
getContentResolver().delete(RawContacts.CONTENT_URI, selection, null);

【讨论】:

  • myAccountType 应该是什么?当我将它的值设置为“公司”时,没有添加任何联系人。
  • 通常是你的包名+一些东西,例如com.company.android.sync-account
  • account-name 通常用于用户特定的帐户,例如对于 Google 帐户,它可能类似于:ACCOUNT_TYPE = com.google.sync-account, ACCOUNT_NAME user-name@gmail.com
  • 我尝试了各种删除方法,但都没有奏效。你能推荐一种替代方法吗?
猜你喜欢
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多