0

I want to get all phone contacts from device in android.i have used the following code.but the problem is it takes more time to return the results.is there any solution?

ContentResolver cr = getContentResolver();
        int index=0;
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        if (cur.getCount() > 0) 
        {
            phoneNames=new String[cur.getCount()];
            phoneNumbers=new String[cur.getCount()];
        while (cur.moveToNext())
        {
            String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
             name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));



        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {

             phoneNames[index]=name;
            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                    new String[]{id}, null);


                    while (pCur.moveToNext()) 
                    {
                        phoneIndex++;
                        phoneNumbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        index++;
                    } 
                    pCur.close();

            }     
        }  
Kakey
  • 3,936
  • 5
  • 29
  • 45
  • Do you mean it takes a lot of time to execute this code? – Dalmas Feb 03 '11 at 18:01
  • You have n^3 complexity here with n^2 different database transactions. You can almost certainly do this all in a single database query. – Falmarri Feb 03 '11 at 18:56

2 Answers2

2

After reading the code i assume that what you want is a list of contacts with DISPLAY NAMES and their respective phone numbers.

If you are specifically looking for data related to phone numbers i suggest you query on android.provider.ContactsContract.PhoneLookup and fetch the results using a single cursor. The following are the fields that you would be interested in: DISPLAY_NAME HAS_PHONE_NUMBER NUMBER TYPE

e.g

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
 resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...

Further details please refer this

Please post your requirement if the assumptions are not true.

  • Some of quick checks:

    1. Select only the required columns and not all in the first query.
    2. Instead of using Integer.parseInt(cur.getString) use cur.getInt()
    3. Use PhoneLookup whenever dealing with phone numbers ( the number field gives the raw phone number instead of the value stored in the database which can contain
      -,),( appended with it)
    4. Avoid using Cursor within a cursor. Use the API's which includes joins already implemented in it like RawContactsEntity, PhoneLookup.

Hope that helps.

Manish Khot
  • 3,027
  • 2
  • 29
  • 37
0

Don't do complex database queries on the UI thread. What are you trying to do with the results? If you are displaying things in a list, use a CursorAdapter so that you only pull out what you need when you need it.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82