A late answer, but maybe it helps anyway:
If you look at the source code of ContactsProvider and search for "matcher.addURI" (don't be surprised if it stops loading in the middle... it resumes loading after a minute or two), then you see that it has a finite set of URI schemes that it can handle. It has a handler for "phones/#" but not for "phones", which is what you would need.
This means, there is just no code to delete all phone numbers, you have to get the IDs of all numbers first, and then delete each one at a time. Of course this takes a lot more CPU and memory resources, but at least it works.
The following code deletes a specific number. Please be aware that I did not test this code, but it is 90% identical to the code I use to delete all numbers of a given person, which needs similar treatment beacause Android can't delete "people/#/phones" but "people/#/phones/#"
EDIT: I just realized that I misunderstood the question. I thought you would like to delete the phone number, which my code does. But now I see you want to delete the contanct.
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);
}
}
The code is a bit verbose because it makes two assumptions:
- there could be multiple lines to delete (e.g. the number is stored twice)
- it might be unsafe to get a cursor, delete a row, and keep using the cursor
Both assumptions are handled by first storing the idsToDelete
in an ArrayList
and then deleting.
You might also consider to normalize the number you search for, and use the Column People.Phones.NUMBER_KEY
instead, because it contains the numbers in normalized form.