简体   繁体   中英

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

I am trying to retrieve contacts on an android device that are explicitly saved (ie 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM