Wednesday, December 14, 2011

Delete the whole Contacts from the android mobile

AIM:
To delete all contacts from the contacts of android phone.
Solution:
By executing the below lines will delete  all contacts from phone book permenently.

Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
while (cur.moveToNext()) {
try {
    String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,lookupKey);                            System.out.println("The uri is " + uri.toString());
    int deletedContacts=cr.delete(uri, null, null);
    log.i("","No.of deleted Contacts: "+deletedContacts);
  } catch (Exception e) {
    System.out.println(e.getStackTrace());
           }
This works on android 2.2 and above.
Please dont forget to add these permissions in Manifest.xml

 <uses-permission android:name="android.permission.READ_CONTACTS" />
 <uses-permission android:name="android.permission.WRITE_CONTACTS" />
      

No comments:

Post a Comment

Android Developers Blog

Ram's shared items