2012-04-20 2 views
1

안드로이드에서 연락처를 더 빨리 읽을 수있는 방법이 있습니까? 예를 들어 커서가있는 내 메서드는 30-50 개의 연락처를 읽는 데 3-5 초가 걸립니다. 그것은 매우 길다.빠른 읽기 연락처 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에서 연락처 데이터를 읽을 시간이 걸릴 때문에 빠른 연락처를 읽고

재미있다 하지만 여전히 u는 String [] projection 매개 변수를 managedQuery에 제공하여 성능을 향상시킬 수 있습니다.

널 값을 제공함으로써 레코드의 모든 열을 가져 오기 때문에 contactsContract에서 필요한 데이터 만 가져옵니다.

+0

을 사용 튜토리얼을 완료 90 %의 타이밍을 줄이는 데 도움이 될 것입니다. 또한 [CursorLoader] (http://developer.android.com/reference/android/content/CursorLoader.html)는 UI 스레드에서 작업을 수행하므로 (아직 수행하지 않은 경우) 사용해야합니다. – hwrdprkns

+0

예, 할 수 있습니다 ... :) – Kri

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

이것은 당신이이 사이트 http://www.blazin.in/2016/02/loading-contacts-fast-from-android.html 나는이 답변에 동의