-1

I'm currently doing development on an Android app that requires me to read all the contacts on a device and select only specific contacts based on criteria (only contacts that have at least one valid mobile number and all email addresses linked to that contact).

I've tried the recommended approach at https://stackoverflow.com/a/19563999/3262731, but on a test device with approximately 800 contacts, retrieving all the records and then filtering takes about 17-20 seconds.

Ideally I'd love to build the criteria into a query that joins the contacts, phone, and email store tables in the contacts db as opposed to filtering in my code.

Does anyone have any suggestions please?

TylerH
  • 20,799
  • 66
  • 75
  • 101
thisguy
  • 1
  • 3

2 Answers2

0

According to http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html

Query

  • If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.
  • If you need to look up a contact by the phone number, use PhoneLookup.CONTENT_FILTER_URI, which is optimized for this purpose.
  • If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI URI.
  • If you need to look up a contact by some data element like email address, nickname, etc, use a query against the ContactsContract.Data table. The result will contain contact ID, name etc.
TylerH
  • 20,799
  • 66
  • 75
  • 101
Drakosha
  • 11,925
  • 4
  • 39
  • 52
  • Thanks Drakosha but what i'd like to do is not look up a contact - i'd like to retrieve all contacts (first, last names and mobile numbers) based on whether they have at least 1 mobile number and retrieve all their email addresses as well. – thisguy Aug 30 '14 at 15:26
0

The android documentation seems to contain information in what you're looking for found here.

private static final String[] PROJECTION =
    {
        /*
         * The detail data row ID. To make a ListView work,
         * this column is required.
         */
        Data._ID,
        // The primary display name
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                Data.DISPLAY_NAME_PRIMARY :
                Data.DISPLAY_NAME,
        // The contact's _ID, to construct a content URI
        Data.CONTACT_ID
        // The contact's LOOKUP_KEY, to construct a content URI
        Data.LOOKUP_KEY (a permanent link to the contact
    };

return new CursorLoader(getActivity(), contentUri, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);

More details on how to define your criteria in the documentation. I would think this would be faster than using a ContentResolver as well.

Nelson Hoang
  • 413
  • 2
  • 11
  • Thanks Nelson, that was my starting point but its not exactly what i require and the documentation itself doesn't provide much in the way of clues for getting data from the different contact tables (email, phone) simultaneously. – thisguy Aug 30 '14 at 15:27