Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android delete specific contact programmatically

Tags:

android

Right now, this code is working ops.add(ContentProviderOperation .newDelete(ContactsContract.Data.CONTENT_URI) .withSelection(ContactsContract.Data.RAW_CONTACT_ID+"=?",new String[] {sid}).build());

However it creates unknown record and seems that its the deleted contact. Do I need something to make it work properly?

like image 600
C3pO Avatar asked Jun 12 '12 16:06

C3pO


1 Answers

I made it work this way:

public static boolean deleteContact(Context ctx, String phone, String name) {
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
    Cursor cur = ctx.getContentResolver().query(contactUri, null, null, null, null);
    try {
        if (cur.moveToFirst()) {
            do {
                if (cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)) {
                    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);
                    return true;
                }

            } while (cur.moveToNext());
        }

    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }
    return false;
}
like image 123
user788511 Avatar answered Oct 04 '22 17:10

user788511