Android set contact to favorite programmatically

时光总嘲笑我的痴心妄想 提交于 2019-12-13 01:34:13

问题


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



来源:https://stackoverflow.com/questions/23932129/android-set-contact-to-favorite-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!