6

我在 android 2.1 ContactContract 工作,当我没有将帐户(例如:gmail 帐户)设置为 android 模拟器时,新建了一个联系人,但无法在 DB 中删除此联系人。

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());
4

3 回答 3

12

从 RawContacts 中删除联系人将删除 Data、Contacts 表中的数据。

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);

于 2010-08-05T10:35:25.480 回答
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;
}

注意:完全删除后,此联系人无法同步

于 2017-03-14T11:00:46.057 回答
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);
于 2015-11-28T01:11:37.397 回答