Delete a specific contact in android

In my application, I need to remove a specific CONTACT from the phone address book, but I just received only a specific number, not all the contact was deleted, so please help me with this.

Thanks in advance.

+4
source share
2 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 a specific contact, change the request

 cr.delete(uri, null, null); 

Hope this helps!

+7
source

Also try this code

 private void deletePhoneNumber(Uri peopleUri, String numberToDelete) { Uri.Builder builder = peopleUri.buildUpon(); builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY); Uri phoneNumbersUri = builder.build(); String[] mPhoneNumberProjection = { People.Phones._ID, People.Phones.NUMBER }; Cursor cur = resolver.query(phoneNumbersUri, mPhoneNumberProjection, null, null, null); ArrayList<String> idsToDelete = new ArrayList<String>(); if (cur.moveToFirst()) { final int colId = cur.getColumnIndex(People.Phones._ID); final int colNumber = cur.getColumnIndex(People.Phones.NUMBER); do { String id = cur.getString(colId); String number = cur.getString(colNumber); if(number.equals(numberToDelete)) idsToDelete.add(id); } while (cur.moveToNext()); } cur.close(); for (String id : idsToDelete) { builder.encodedPath(People.Phones.CONTENT_DIRECTORY + "/" + id); phoneNumbersUri = builder.build(); resolver.delete(phoneNumbersUri, "1 = 1", null); } } 

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1346222/


All Articles