Only get android contacts that are saved to the phone or sim

风格不统一 提交于 2019-12-12 01:39:53

问题


I am trying to retrieve contacts on an android device that are explicitly saved (i.e. not the ones returned by gmail or Facebook and other apps that save contact-like information). I have been playing around with this code block from the PhoneGap contacts plugin:

Cursor idCursor = mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
    new String[] { ContactsContract.Data.CONTACT_ID },
    whereOptions.getWhere(),
    whereOptions.getWhereArgs(),
    ContactsContract.Data.CONTACT_ID + " ASC");

I modified various arguments and tried querying based on ContactsContract.RawContacts.ACCOUNT_TYPE with no luck. How can I modify the code block above to achieve what I need?


回答1:


Whether or not the contacts appear in the regular contacts is determined by the following property:

ContactsContract.Contacts.IN_VISIBLE_GROUP

I modified one of the queries to account for this and it removed contacts returned that aren't really contacts (such as gmail history, ...etc):

options.setWhere("(" + ContactsContract.Contacts.IN_VISIBLE_GROUP + " = 1 AND " + ContactsContract.Data.CONTACT_ID + " LIKE ? )");

The query in the original question (from the cordova contacts plugin) simply gets the ids that match the provided search term, the query to modify is the one later in the plugin code (more accurately the corresponding getWhere() function was modified to account for the IN_VISIBLE_GROUP property):

Cursor c = mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
            columnsToFetch.toArray(new String[] {}),
            idOptions.getWhere(),
            idOptions.getWhereArgs(),
            ContactsContract.Data.CONTACT_ID + " ASC");

I hosted a modified version of the plugin on my github. Note that my version only works for instances where there is no search term.



来源:https://stackoverflow.com/questions/24373536/only-get-android-contacts-that-are-saved-to-the-phone-or-sim

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