简体   繁体   English

如何在Android 2.2上删除联系人?

[英]How to delete the contacts on android 2.2?

I need to delete the duplicate contacts and then inserting the new contact on Android 2.2. 我需要删除重复的联系人,然后在Android 2.2上插入新的联系人。

How to do this? 这个怎么做?

give me any sample code or sites for this. 给我任何示例代码或网站。

To delete the content item from android you need a content URI and some deletion criteria. 要从android删除内容项,您需要一个内容URI和一些删除条件。

Every content type has its own content URI. 每个内容类型都有其自己的内容URI。 If you're writing your contacts sync adapter, then you might want to use ContactsContract.RawContacts.CONTENT_URI. 如果要编写联系人同步适配器,则可能要使用ContactsContract.RawContacts.CONTENT_URI。

Other thing you need is a ContentResolver - an interface to communicate with a content provider (an operations, such as insert, update and delete are defined in this interface). 您需要的另一件事是ContentResolver-与内容提供者进行通信的接口(此接口中定义了诸如插入,更新和删除之类的操作)。 You can get ContentResolver by calling getContentResolver from your application context. 您可以通过从应用程序上下文调用getContentResolver来获取ContentResolver。

So, here is the snippet of code that should delete ALL the contacts (not tested it though): 因此,这是应该删除所有联系人的代码段(尽管未测试):

ContentCesolver cr = getContentResolver();
URI uri = RawContacts.CONTENT_URI;
cr.delete(uri, null, null);

Note that when you're using RawContacts.CONTENT_URI, the contact item is not deleted. 请注意,当您使用RawContacts.CONTENT_URI时,联系人项不会被删除。 Instead it is only marked for deletion. 而是仅将其标记为删除。 To delete it completely you should add a ContactsContract.CALLER_IS_SYNCADAPTER parameter to your URI: 要完全删除它,您应该在您的URI中添加一个ContactsContract.CALLER_IS_SYNCADAPTER参数:

uri.buildUpon()
   .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER,
         "true").build()

For further explanation read official docs about content providers . 有关进一步的说明,请阅读有关内容提供者的官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM