2012-04-20 84 views
1

有沒有更快的方法來讀取android中的聯繫人?例如,我用光標的方法需要3-5秒才能讀取30-50個聯繫人。這很長。快速閱讀聯繫人android

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

任何想法?

回答

2

你的問題很有趣....

讀快速接觸,因爲它需要時間來讀取ContactsContract聯繫人數據..

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

僅提取那些您需要從contactsContract中獲取的數據,因爲通過提供空值可以提取記錄的所有列。

+0

我這個答案達成一致。此外,你應該使用[CursorLoader](http://developer.android.com/reference/android/content/CursorLoader.html),因爲它將完成UI線程的工作(如果你還沒有這樣做的話)。 – hwrdprkns 2012-04-20 17:32:06

+0

是的,你可以做到這一點... :) – Kri 2012-04-20 17:35:10

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個聯繫人,它速度很快。因爲它不容易以秒爲單位獲取所有聯繫人及其所有數據。

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