M
M
MODifikaTOR2017-04-19 09:11:34
Java
MODifikaTOR, 2017-04-19 09:11:34

Why does not remove the number from the contact?

I am making a contact book, it is necessary to implement the removal of a phone number from a contact. On the stack, they advised me the code:

private final String queryPhone = ContactsContract.Contacts.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Contacts.Data.MIMETYPE + "='" 
+ ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND ";
private final String queryByNumber = ContactsContract.CommonDataKinds.Phone.NUMBER + "=?";

    public void removePhoneByNumber(Long contactId, String phoneNumber) {
        ArrayList<ContentProviderOperation> os = new ArrayList();
        String[] phoneArgs = new String[] {String.valueOf(contactId), phoneNumber};
        os.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
                .withSelection(queryPhone + queryByNumber, phoneArgs)
                .build());
        try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, os);
        }
        catch (RemoteException e) {
            e.printStackTrace();
        }
        catch (OperationApplicationException e) {
            e.printStackTrace();
        }
    }

The author of the code says that the code works for him, but I have no execution result. The function is called from the delete button listener. What would you recommend to change? Or is there another way to remove a number from a contact?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Destplay, 2017-04-19
@Destplay

<uses-permission android:name="android.permission.READ_CONTACTS"
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
Did you write it in the manifest?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question