I am working on a project that creates random contacts but I am having trouble implementing the deletion capability. I have marked each generated contact with a note through
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE)
.withValue(Note.NOTE, note)
.build());
Now to delete the contacts i need to use something similar to what i have found here
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cur = ctx.getContentResolver().query(contactUri, null, null,
null, null);
try {
if (cur.moveToFirst()) {
do {
String lookupKey =
cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI,
lookupKey);
ctx.getContentResolver().delete(uri, null, null);
} while (cur.moveToNext());
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return false;
}
I believe the only difference between the code above and what I am searching for is that the one above uses a phone number to delete the contact but for my purposes I need to delete by the Note field of the contact.
Fixed my problem..I am too new to java and tried comparing strings using '==' instead of stringObject.equals().
here is a little function to delete contacts based on their string in case anyone needs something like that.