简体   繁体   中英

Fast reading contacts android

Is there a faster method for reading contacts in android? For example my method with cursor take 3-5 seconds for reading 30-50 contacts. It's very long.

        Cursor cursor =  managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);      
           while (cursor.moveToNext()) 
           {           
               String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

               String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

               if ( hasPhone.equalsIgnoreCase("1"))
                   hasPhone = "true";
               else
                   hasPhone = "false" ;

               if (Boolean.parseBoolean(hasPhone)) 
               {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                while (phones.moveToNext()) 
                {
                  names.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))); 
                  numbers.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                }
                phones.close();
               }
          }     

Any ideas?

your question is interesting....

reading a fast contacts because it take time to read contacts data from ContactsContract..

i don't know about another way then the one you use, but still u can increase the performance by providing a String[] projection parameter to managedQuery...

fetch only those data which u require from contactsContract, because by providing null value it fetches all the columns of the record.

Sorry for my bad english. I have created the similar but with different style using HashMap. I will paste my approach.

            //Create a hashMap, Key => Raw Contact ID and value => Object of customClass

            HashMap<Long, ContactStructure> mFinalHashMap = new HashMap<Long, ContactStructure>();

            //Run IN query in data table. example

             select mimetype_id, raw_contact_id, data1 to data14 from data where raw_contact_id IN (select _id from raw_contacts where deleted <> 1 and account_type = "phone" and account_name = "bla bla") and mimetype_id = (select _id from mimetypes where mimetype = "vnd.something.phone");

Now Create a class which will have all the data of contact.

while accessing the cursor.

            while (cursor.moveToNext()) {
                ContactStructure contactStructure = mFinalHashMap.get(rawContactID);
        //It will return the previous instance of object, If we already put
                    if(rawContactStructure == null) {
                        contactStructure = ContactStructure.provideInstance();
                    }

    //Now check for your required mimeType
                           case MIMETYPE_PHONE:
                    contactStructure.hasPhoneNo = true;
                    contactStructure.phoneNumbers.add(addDetail); //add the data1 .. to data14
                break;

            }

    /*Demo class for saving the details*/
    public class ContactMetaData {
        static classContactStructure {
                   boolean          hasPhoneNo;
            List<List<String>>  phoneNumbers; 
    public static ContactStructure provideInstance() {
contact.phoneNumbers = new ArrayList<List<String>>();
            ContactStructure contact = new RawContactStructure();
return contact
    }

    }

use this approach, I tried with 3000 contacts with all the data, It was fast. coz its not easy to get all the contacts and their all data with in sec..

For reading contacts faster you need to use the concept of the projection where you specify only columns which you need to fetch.

  cursor.moveToFirst();
    while (cursor.isAfterLast() == false) {

        String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
        int contactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Log.d("con ", "name " + contactName + " " + " PhoeContactID " + phoneContactID + "  ContactID " + contactID)

        cursor.moveToNext();
    }

This will help you to reduce the timing by 90% for complete tutorial i used this site http://www.blazin.in/2016/02/loading-contacts-fast-from-android.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM