无法使用 ContentProviderOperation 删除整个联系人

发布于 2024-09-06 03:24:03 字数 1204 浏览 2 评论 0原文

我一直在处理 Android 联系人。我可以向他们展示并更新,但是 当我想删除任何一个时,它都没有完全删除。在通讯录中 应用程序显示为(未知),没有任何数据。这是我的例子:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(Data.CONTACT_ID + "=?", new String[]{selectedid})
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

我应该采取其他措施来完全删除联系人吗? 这些代码似乎删除了表 ContactsContract.Data 中的信息,但它没有删除表 ContactsContract.Contacts 或 ContactsContract.RawContacts 中相关的元素。我怎样才能完全删除联系人?

=================================================== ============================

另外,我尝试了已弃用的方法。它确实有效,但我不想这样做。这是示例代码:

ContentResolver contentResolver = m_cContent.getContentResolver();
contentResolver.delete(People.CONTENT_URI, People.NAME + "=?", new String[] { SelectedName });

如果我将此代码修改为

ContentResolver contentResolver = m_cContent.getContentResolver();
contentResolver.delete(ContactsContract.Contacts, ContactsContract.Contacts._ID + "=?", new String[] { Selectedid });

它没有效果。

这是否意味着只能通过姓名而不是 ID 来删除联系人? 我到底该怎么做才能删除联系人?

谢谢, 锚定

I have been working with Android contacts. I am able to show them, update but
when I want to delete any, it is not deleted completely. In Contacts
application is shown as (Unknown) without any data. Here is my example:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(Data.CONTACT_ID + "=?", new String[]{selectedid})
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

Should I do anything else to delete contact entirely?
It seems that these code delete info in table ContactsContract.Data but it does not delete element related in table ContactsContract.Contacts or ContactsContract.RawContacts.how can i do to delete an contact completely?

============================================================================

also, i tried deprecated method. It dose work, but i do not want to do so. Here is the sample code:

ContentResolver contentResolver = m_cContent.getContentResolver();
contentResolver.delete(People.CONTENT_URI, People.NAME + "=?", new String[] { SelectedName });

and if i modify this code to

ContentResolver contentResolver = m_cContent.getContentResolver();
contentResolver.delete(ContactsContract.Contacts, ContactsContract.Contacts._ID + "=?", new String[] { Selectedid });

It has no effect.

Does it mean that one can only delete a contact by name instead of by its id?
What on earth can i do to delete contact?

Thanks,
Enchor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2024-09-13 03:24:03

您正尝试通过从数据表中删除联系人的数据行来删除该联系人。
那是行不通的。

一个联系人由多个原始联系人组成,每个原始联系人的数据都保存在Data表中。
删除联系人时,所有原始联系人及其数据也会被删除。

这样做:

long contactId = 12345;
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, String.valueOf(contactId));
int deleted = getContentResolver().delete(contactUri, null, null);

如果操作成功,deleted 将为 1。

You are trying to delete a contact by deleting its data rows from the Data table.
That wouldn't work.

A contact is made up of several raw-contacts, each raw-contact has its data saved in the Data table.
When deleting a contact, all raw-contacts get deleted as well, along with their data.

Do this:

long contactId = 12345;
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, String.valueOf(contactId));
int deleted = getContentResolver().delete(contactUri, null, null);

deleted will be 1 if the operation succeeded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文