快速阅读联系android

时间:2012-04-20 17:07:51

标签: android android-contacts

在Android中有没有更快的方法来阅读联系人?例如,使用光标的方法需要3-5秒才能读取30-50个联系人。这很长。

        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();
               }
          }     

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

你的问题很有意思......

阅读快速联系人,因为从ContactsContract中读取联系人数据需要时间。

我不知道你使用的另一种方式,但你仍然可以通过为managedQuery提供String []投影参数来提高性能...

仅从contactsContract中获取您需要的数据,因为通过提供空值,它会获取记录的所有列。

答案 1 :(得分:2)

抱歉我的英语不好。我使用HashMap创建了类似但不同的样式。我会粘贴我的方法。

            //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");

现在创建一个包含所有联系人数据的类。

访问光标时

            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
    }

    }

使用这种方法,我尝试了3000个联系人的所有数据,这很快。因为它很难在第二节获得所有联系人及其所有数据。

答案 2 :(得分:0)

为了更快地阅读联系人,您需要使用投影的概念 您只需指定需要获取的列。

  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();
    }

这将帮助您将使用此网站的完整教程缩短90%的时间http://www.blazin.in/2016/02/loading-contacts-fast-from-android.html