Alex
Alex

Reputation: 131

What is the efficient way to get all contacts names and phone numbers in the contact API

I currently use the old contacts API (that was deprecated on Android 2.0), and I am wondering if the is a way to get all contacts along with their's phone number, without making a separate query for each contact as was advised in a few sites I found.

for instance in the old API, I could do something like this:

String[] projection = new String[] { Phones._ID, Phones.NAME,
            Phones.NUMBER };

    Uri contacts = Phones.CONTENT_URI;

    Cursor managedCursor = managedQuery(contacts, 
            projection, // Which columns to return
            null, // Which rows to return (all rows)
            null, // Selection arguments (none)
            // Put the results in ascending order by name
            Phones.NAME + " ASC");

thanks.

Upvotes: 2

Views: 1996

Answers (1)

Alex
Alex

Reputation: 131

I found what I was looking for, and using the new API is even simpler, here is the new way of querying all the contact along with their names and phone number:

Cursor managedCursor = getContentResolver()
    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
     new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null,  Phone.DISPLAY_NAME + " ASC");

Upvotes: 4

Related Questions