Note that there are some explanatory texts on larger screens.

plurals
  1. POFastest way to query contacts with "mobile phone numbers" on Android
    primarykey
    data
    text
    <p>I need to get a distinct list of all contacts from a device and their phone numbers. But wait... We know that some contacts may have multiple numbers assigned, it all depends how each of the users stores his contacts. Here is what i do:</p> <pre><code> ContentResolver cr = context.getContentResolver(); Uri uri = ContactsContract.Contacts.CONTENT_URI; String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'"; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; ArrayList&lt;user&gt; contacts = new ArrayList&lt;user&gt;(); Cursor users = a.managedQuery(uri, projection, selection, null, sortOrder); while (users.moveToNext()) { user u = new user(); u.PhoneId = users.getInt(users.getColumnIndex(ContactsContract.Contacts._ID)); u.Name = users.getString(users.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String homePhone = "", cellPhone = "", workPhone = "", otherPhone = ""; Cursor contactPhones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + u.PhoneId, null, null); while (contactPhones.moveToNext()) { String number = contactPhones.getString(contactPhones.getColumnIndex(Phone.NUMBER)); int type = contactPhones.getInt(contactPhones.getColumnIndex(Phone.TYPE)); switch (type) { case Phone.TYPE_HOME: homePhone = number; break; case Phone.TYPE_MOBILE: cellPhone = number; break; case Phone.TYPE_WORK: workPhone = number; break; case Phone.TYPE_OTHER: otherPhone = number; break; } } u.Phone = ((cellPhone!="") ? cellPhone : ((homePhone!="") ? homePhone : ((workPhone!="") ? workPhone : otherPhone))); } return contacts; </code></pre> <p>The process works but for my 80 contacts it takes between 1000-2000 miliseconds, and i need to work much faster :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload