1. Contacts Retrieval (< Android 2.0)

When using Android SDK lower than Level 5 (1.6, 1.5, ...), we can use below code to query the contacts from the ContentProvider.
import android.provider.Contacts.People;
import android.database.Cursor;

// Form an array specifying which columns to return.
String[] projection = new String[] {
                             
People._ID,
                             
People._COUNT,
                             
People.NAME,
                             
People.NUMBER
                         
};

// Get the base URI for the People table in the Contacts content provider.
Uri contacts =  People.CONTENT_URI;

// Make the query.
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
                         
People.NAME + " ASC");
Above code can obtain the name, ID and the primary phone number of all contacts, By the contact ID, we can query the phone numbers (all numbers), emails, photos, etc. But it can only obtain such informations contact by contact, it is very time consuming.
if (managedCursor != null && managedCursor.moveToFirst()) {
    do {
       
if (cur.moveToFirst()) {
           
String name;
           
long id;
            
int nameColumn = cur.getColumnIndex(People.NAME);
           
int idColumn = cur.getColumnIndex(People._ID);
   
       
do {
           
// Get the field values
            name
= cur.getString(nameColumn);
            id
= cur.getLong(idColumn);
            Cursor phoneCur = contentResolver.query(Phone.CONTENT_URI, ....);
            if (phoneCur != null) {                 if (phoneCur.moveToFirst()) {                     do {                         ...                     while (phoneCur.moveToNext());                 }                 phoneCur.close();             }
        } while (cur.moveToNext());

   
}
}
I have tried to find a way to speed up the procedure, but failed. Finally, I give up using SDK < Level 5 and using Android 2.0 API (Level 5).

Comments