Android set contact to favorite programmatically

2019-08-10 18:04发布

问题:

Can any one told me if I have a list of contact and read them in my application and I want to set contact to favorite from my application directly, so that when I open my phone contact again I will be able to find contact in the favorite list of android phone.

Please help

回答1:

you must add permission to your application to be able to write to the contact content provider. android.permission.WRITE_CONTACTS android.permission.READ_CONTACTS

After that you need to update the value for the STARRED field.

ContentValues v = new ContentValues(); v.put(ContactsContract.Contacts.STARRED,1); getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, v, ContactsContract.Contacts.Data.DATA1+"=?", new String[]{putThePhoneNumberHere+""});



回答2:

You need to update value STARRED in contacts database from 0 to 1.

Something like:

values.put(Contacts.STARRED, 1);

getContentResolver().update(Contacts.CONTENT_URI, values, Contacts.DISPLAY_NAME + "= ?", strNamevalue);

This is a sql query:

UPDATE %Contacts.CONTENT_URI% SET STARRED = 1 WHERE %Contacts.DISPLAY_NAME% = %strNamevalue% 

Values in %% should be replaced by valid table name and where clause params

Hope it helps