Set SEND_TO_VOICEMAIL on Android contacts

2019-05-10 07:21发布

问题:

I'm trying to modify the value of SEND_TO_VOICEMAIL from 0 to 1 and vice versa. I succeed to modify the others contact's details, such as name, number, nickname, email, ecc... but I need to change SEND_TO_VOICEMAIL.

I tried many possibility, but this should work... i guess:

        String rawContactId = "1";

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(
            ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection( Data.RAW_CONTACT_ID + "=?" , new String[] { rawContactId })
            .withValue(ContactsContract.RawContacts.SEND_TO_VOICEMAIL , 1)
            .build()
        );

        try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }

but the logcat says:

ERROR/AndroidRuntime(822): android.database.sqlite.SQLiteException: no such column: send_to_voicemail: , while compiling: UPDATE data SET send_to_voicemail=? WHERE _id =?

I really don't know what to try anymore. Any help would be really appreciated. Thanks.

回答1:

According to the[manual, the SEND_TO_VOICEMAIL field you're looking for isn't in the ContactsContract.Data table but in the ContactsContract.Contacts table. It seems like you're trying to modify the wrong table.

See ContactsContract.Data and ContactsContract.Contacts for the fields in each table.



回答2:

It is better modify through ContentProvider Contacts.CONTENT_URI:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Contacts.CONTENT_URI)
    .withSelection(Contacts._ID + "=?", new String[]{hmout.get("cid").toString()})
    .withValue(Contacts.SEND_TO_VOICEMAIL, 1)
    .build());

try { 
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) { 
    Log.e("Exception: ", e.getMessage()); 
}