从 android 个联系人中删除联系人

Delete contact from android contacts

我正在尝试从 phone 个联系人中删除一个联系人。该联系人已从 phone 个联系人中删除,但未从服务器端(Google 个联系人)中删除,并且当 Google 个联系人同步触发时,删除的联系人会重新出现。下面是我的代码。

public static void deleteContact(long rawid, ContentResolver contentResolver) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    Uri uri = ContactsContract.RawContacts.CONTENT_URI
            .buildUpon()
            .appendQueryParameter(
                    ContactsContract.CALLER_IS_SYNCADAPTER,
                    "true")
            .build();
    ops.add(ContentProviderOperation
            .newDelete(uri)
            .withSelection(
                    ContactsContract.RawContacts._ID + " = ?",
                    new String[]{Long.toString(rawid)})
            .build());

    try {
        contentResolver.applyBatch(
                ContactsContract.AUTHORITY,
                ops);
    } catch (RemoteException | OperationApplicationException e) {
        e.printStackTrace();
    }
} 

您应该尝试在您的代码中使用 ContactsContract.CALLER_IS_SYNCADAPTER 作为 false。设置为 true 时,联系人将从数据库中永久删除。但是当下一次同步发生时,联系人会同步回来。 Google 同步检查已删除联系人的方式是使用已删除标志,该标志仅在您将 ContactsContract.CALLER_IS_SYNCADAPTER 设置为 false 时设置。下面是来自 ContactsProvider class(联系人数据存储的内容提供者)

的代码片段
if (callerIsSyncAdapter || rawContactIsLocal(rawContactId)) {
    // When a raw contact is deleted, a SQLite trigger deletes the parent contact.
    // TODO: all contact deletes was consolidated into ContactTableUtil but this one can't
    // because it's in a trigger.  Consider removing trigger and replacing with java code.
    // This has to happen before the raw contact is deleted since it relies on the number
    // of raw contacts.
    db.delete(Tables.PRESENCE, PresenceColumns.RAW_CONTACT_ID + "=" + rawContactId, null);
    count = db.delete(Tables.RAW_CONTACTS, RawContacts._ID + "=" + rawContactId, null);
    mTransactionContext.get().markRawContactChangedOrDeletedOrInserted(rawContactId);
} else {
    count = markRawContactAsDeleted(db, rawContactId, callerIsSyncAdapter);
}