Surinder Singh Bindra
Surinder Singh Bindra

Reputation: 61

How to delete all android contacts that start with a specific name?

I want to delete all contacts from my android phone that starts with a "AAA" or that contains "AAA" . Here's what I tried:

private void deleteContact(String name) {

        ContentResolver cr = getContentResolver();
        String where = ContactsContract.Data.DISPLAY_NAME + " = ? ";
        String[] params = new String[] {"AAAA"};

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
                .withSelection(where, params)
                .build());
        Log.e(",,,,",String.valueOf(ops.get(0)));
        try {


            cr.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Toast.makeText(NativeContentProvider.this, "Deleted the contact with name '" + name +"'", Toast.LENGTH_SHORT).show();

    }

but failed. Please give me some idea so I can proceed further in my project.

Upvotes: 1

Views: 1029

Answers (1)

Baker
Baker

Reputation: 28020

Maybe this will get you on the right track:

ContentResolver mContentResolver = getContentResolver();
    private int deleteContactsLike(String name) {
    return mContentResolver.delete(
            ContactsContract.RawContacts.CONTENT_URI,
            ContactsContract.Contacts.DISPLAY_NAME
            + " like ?",
            new String[] { name + '%'});

Upvotes: 2

Related Questions