Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete specific contact in android

Tags:

android

In my application, I require to delete a particular CONTACT from phone address book, but I just got only specific number deleted not whole contact, so please help me in this.

Thanks in advance.

like image 862
Nikunj Avatar asked Feb 25 '23 15:02

Nikunj


1 Answers

To delete all contacts use the following code ;

ContentResolver cr = getContentResolver();
    Cursor cur = cr.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());
            cr.delete(uri, null, null);
        }
    catch(Exception e)
    {
        System.out.println(e.getStackTrace());
    }
}

To delete any specific contact modify the query

cr.delete(uri, null, null);

Hope it helps !!

like image 181
Kartik Bhatt Avatar answered Mar 07 '23 10:03

Kartik Bhatt