1

I need to delete the duplicate contacts and then inserting the new contact on Android 2.2.

How to do this?

give me any sample code or sites for this.

bharath
  • 14,283
  • 16
  • 57
  • 95

2 Answers2

3

To delete the content item from android you need a content URI and some deletion criteria.

Every content type has its own content URI. If you're writing your contacts sync adapter, then you might want to use 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). You can get ContentResolver by calling getContentResolver from your application context.

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. Instead it is only marked for deletion. To delete it completely you should add a ContactsContract.CALLER_IS_SYNCADAPTER parameter to your URI:

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

For further explanation read official docs about content providers.

Vanuan
  • 31,770
  • 10
  • 98
  • 102
  • In order to delete multiple specific contacts (given their contact keys, for example), is it ok to use getContentResolver().applyBatch ? Or should I create a single delete operation which is a bit complex? What's better, or what's the advantages&disadvantages of each? – android developer Jan 13 '16 at 08:15
  • Sorry, I don't know. – Vanuan Jan 13 '16 at 21:03
2

May be this will help you,

How to remove a contact programmatically in android

Community
  • 1
  • 1
Mudassir
  • 13,031
  • 8
  • 59
  • 87