2012-04-20 89 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