簡體   English   中英

快速閱讀聯系android

[英]Fast reading contacts android

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

有任何想法嗎?

你的問題很有趣......

閱讀快速聯系人,因為從ContactsContract讀取聯系人數據需要時間。

我不知道你使用的另一種方式,但你仍然可以通過為managedQuery提供String []投影參數來提高性能...

僅從contactsContract中獲取您需要的數據,因為通過提供空值,它將獲取記錄的所有列。

對不起,我的英語不好。 我使用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個聯系人的所有數據,它很快。 因為它不容易得到所有的聯系人和他們的所有數據與秒..

要更快地閱讀聯系人,您需要使用投影的概念,其中您只需指定要獲取的列。

  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM