如何删除联系人?

时间:2010-08-05 09:54:27

标签: android contacts delete-row

我正在使用android 2.1 ContactContract,当我没有设置帐户(例如:gmail帐户)到android模拟器然后,新建一个联系人,但无法在数据库中删除此联系人。

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    String[] args = new String[] {id};
    ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
            .withSelection(Data.CONTACT_ID + "=?", args)
            .build());
    ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI)
             .withSelection(RawContacts.CONTACT_ID + "=?", args)
             .build());
    ops.add(ContentProviderOperation.newDelete(Contacts.CONTENT_URI)
             .withSelection(Contacts._ID + "=?", args)
             .build());

3 个答案:

答案 0 :(得分:11)

从RawContacts删除联系人将删除数据,联系人表格中的数据。

ArrayList ops = new ArrayList(); String[] args = new String[] {id}; 
// if id is raw contact id
ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection(RawContacts._ID + "=?", args) .build()); 
    OR
// if id is contact id
ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection(RawContacts.CONTACT_ID + "=?", args) .build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

答案 1 :(得分:2)

public static boolean fullDeleteContactByRawId(String rawId)
{
    Uri rawUri = RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
    String where = RawContacts._ID + " = ?";
    String[] args = new String[]{rawId};

    try
    {
        ContentManager.delete(rawUri, where, args);
    }
    catch(Exception e)
    {
        return false;
    }

    return true;
}

通知: 完全删除后,此联系人无法同步

答案 2 :(得分:0)

我用它来删除现有联系人的电话号码,但不删除联系人本身:

    ArrayList ops = new ArrayList();
    String[] args = new String[]{
        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
        number,
        Integer.toString(ContactsContract.CommonDataKinds.Phone.TYPE_MAIN),
        raw_contact_id
    };
    ops.add(
        ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
            .withSelection(ContactsContract.Data.MIMETYPE + "=? AND "
                + ContactsContract.CommonDataKinds.Phone.NUMBER + "=? AND "
                + ContactsContract.CommonDataKinds.Phone.TYPE + "=? AND "
                + ContactsContract.Data.RAW_CONTACT_ID + "=?"
                , args)
            .build());

    c.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);