How to delete a contact in android?

I want to delete the username Leo. Therefore, I submit a removal request as follows

    int i = getContentResolver().delete(Contacts.CONTENT_URI, Contacts.DISPLAY_NAME +"= 'Leo'",null);
    System.out.println("rows deleted "+i);
but it returns "rows deleted 0"

what is wrong with him.

Editing: The above does not work, because the field is only read using Contacts.CONTENT_URI you can see using the following URI. http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html

+3
source share
3 answers

http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

In the link above, I can find a solution. I can completely delete contacts.

The problem is with the sync adapter.

+1
source

To remove selected contacts from a DB contact, we can use the following code.

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

                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
                ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
                        .withSelection(where, params)
                        .build());
                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();
                }
+1

Suppose you have to specify Android permission on WRITE_CONTACTS. Take a look at http://developer.android.com/reference/android/Manifest.permission.html

0
source

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


All Articles