Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete all contacts in contact list on android mobile programatically

Tags:

android

I want to delete all the contact on one button press from my application so can any one nice person can told me how to delete all the contacts from android mobile programmatically on only one button press? answer would be greatly appreciated...

Actually I was surfing from a couple of hours but not got any appropriate answer. That's why I need to post my problem in this nice forum...thanks to such forum....

like image 831
ram ji Avatar asked Aug 06 '11 11:08

ram ji


2 Answers

It is very simple, This code will remove all your contacts.

ContentResolver contentResolver = <your app context>.getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            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);
        }

Done.

like image 139
Nimit Avatar answered Oct 16 '22 16:10

Nimit


Specify READ_CONTACTS and WRITE_CONTACTS permissions in your AndroidManifest.xml.

Iterate through each contact and delete each record: Content Providers

Contacts

Be careful with deleting Contacts! Deleting an aggregate contact deletes all constituent raw contacts. The corresponding sync adapters will notice the deletions of their respective raw contacts and remove them from their back end storage.

With regards to permissions, see Permission groups:

Caution: Future versions of the Android SDK might move a particular permission from one group to another. Therefore, don't base your app's logic on the structure of these permission groups.

For example, READ_CONTACTS is in the same permission group as WRITE_CONTACTS as of Android 8.1 (API level 27). If your app requests the READ_CONTACTS permission, and then requests the WRITE_CONTACTS permission, don't assume that the system can automatically grant the WRITE_CONTACTS permission.

Note: Your app still needs to explicitly request every permission it needs, even if the user has already granted another permission in the same group. In addition, the grouping of permissions into groups may change in future Android releases. Your code shouldn't have logic that depends on a set of particular permissions being in the same group.

like image 27
CrackerJack9 Avatar answered Oct 16 '22 16:10

CrackerJack9