我正在尝试从手机通讯录中删除联系人。该联系人会从手机通讯录中删除,但不会从服务器端(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();
}
}
答案 0 :(得分:1)
您应该在代码中使用ContactsContract.CALLER_IS_SYNCADAPTER
作为false
。设置为true时,将从数据库中永久删除联系人。但是当下一次同步发生时,联系人将被同步回来。 Google同步检查已删除的联系人的方式是使用已删除的标记,仅当您将ContactsContract.CALLER_IS_SYNCADAPTER
设置为false
时才会设置该标记。以下是ContactsProvider类的代码片段(联系人数据存储区的contentprovider)
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);
}