gpt4 book ai didi

安卓通讯录删除

转载 作者:行者123 更新时间:2023-11-29 17:34:32 28 4
gpt4 key购买 nike

我有一种方法可以从联系人中删除特定号码而不是整个联系人。 (例如,如果一个名字有 2 个联系人,我让用户只删除一个号码,而不是整个号码)。因此,对于包含单个号码的联系人,删除单个号码后,该联系人仅保留姓名而没有关联号码。要使用下面给出的方法删除此类即时消息,该方法工作正常,并且删除了空联系人,但它并不总是有效。它有时行为不端,空的联系仍然存在。有时它会起作用。它总是适用于包含多个号码的名称(删除号码时,使用以下方法也删除空联系人)。对于包含单个号码的姓名(联系人),它可以正常工作并删除空联系人(在号码被删除后),但只是有时。

以下是我如何从联系人中删除“只是一个数字”。

 public void updateContact(String contactId, String type) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();


/* String selectPhone = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND " + ContactsContract.CommonDataKinds.Phone.TYPE + "=?";
String[] phoneArgs = new String[]{contactId, type};

ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).withSelection(selectPhone, phoneArgs).build());

*/
ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.RawContacts.Data._ID + "=? and " + ContactsContract.RawContacts.Data.MIMETYPE + "=?", new String[]{String.valueOf(contactId), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE})
.build());



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

这是我用来删除空联系人的方法,我在 asynTask 中使用上述联系人删除号码后调用此方法。

   public void emptyRemover(String s, String name) {
ContentResolver contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
if (cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)
/*||cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)).equalsIgnoreCase(s)*/){
if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))==0) {
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
contentResolver.delete(uri, null, null);
}
}
}
cursor.close();

}

最佳答案

ContactsContract.RawContacts 删除原始联系人时,您必须考虑:

The invocation of resolver.delete(...), does not immediately delete a raw contacts row. Instead, it sets the DELETED flag on the raw contact and removes the raw contact from its aggregate contact. The sync adapter then deletes the raw contact from the server and finalizes phone-side deletion by calling resolver.delete(...) again and passing the CALLER_IS_SYNCADAPTER query parameter. Some sync adapters are read-only, meaning that they only sync server-side changes to the phone, but not the reverse. If one of those raw contacts is marked for deletion, it will remain on the phone. However it will be effectively invisible, because it will not be part of any aggregate contact.

这就是为什么有时您会看到它被有效删除而有时却看不到的原因。因此,您必须调用两次删除操作,如下所示:

Uri rawUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();    

ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).
withSelection(ContactsContract.RawContacts._ID + "=? AND "
+ContactsContract.RawContacts.ACCOUNT_TYPE+ "=? AND "
+ContactsContract.RawContacts.ACCOUNT_NAME+ "=?"
,params).build()); //sets deleted flag to 1

ops.add(ContentProviderOperation.newDelete(rawUri).
withSelection(ContactsContract.RawContacts._ID + "=? AND "
+ContactsContract.RawContacts.ACCOUNT_TYPE+ "=? AND "
+ContactsContract.RawContacts.ACCOUNT_NAME+ "=?"
,params).build()); //erases

希望对您有所帮助!

关于安卓通讯录删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31217928/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com