简体   繁体   English

快速阅读联系android

[英]Fast reading contacts android

Is there a faster method for reading contacts in android? 在Android中有没有更快的方法来阅读联系人? For example my method with cursor take 3-5 seconds for reading 30-50 contacts. 例如,使用光标的方法需要3-5秒才能读取30-50个联系人。 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.. 阅读快速联系人,因为从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... 我不知道你使用的另一种方式,但你仍然可以通过为managedQuery提供String []投影参数来提高性能...

fetch only those data which u require from contactsContract, because by providing null value it fetches all the columns of the record. 仅从contactsContract中获取您需要的数据,因为通过提供空值,它将获取记录的所有列。

Sorry for my bad english. 对不起,我的英语不好。 I have created the similar but with different style using HashMap. 我使用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. 使用这种方法,我尝试了3000个联系人的所有数据,它很快。 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 这将帮助您将完成教程的时间缩短90%我使用此站点http://www.blazin.in/2016/02/loading-contacts-fast-from-android.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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