2015-01-09 2 views
-1

나는 장치 주소록에서 내 응용 프로그램 데이터베이스로 세부 주소 (Name,Mobile number,DOB,anniversary and email address)을 가진 모든 연락처를 가져 오기위한 코드를 작성했습니다. 코드는 잘 실행되고 모든 세부 정보를 얻지 만 많은 수의 연락처에 대해서는 매우 느린 프로세스입니다. 예를 들어 에 8000 contacts 다음되어 내가 사용하고 20 것이다 5 분 코드 개까지 취주소록에서 연락처 가져 오기가 안드로이드에서 느립니다?

ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
        null, null, null); 
int count = cur.getCount(); 
if (cur.getCount() > 0) { 
    while (cur.moveToNext()) { 
first_name = ""; 
first_name = ""; 

        name = cur 
          .getString(cur 
            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

        first_name = name == null ? "" : name; 


        // birthday 
        Cursor bdc = cr 
          .query(android.provider.ContactsContract.Data.CONTENT_URI, 
            new String[] { Event.DATA }, 
            android.provider.ContactsContract.Data.CONTACT_ID 
              + " = " 
              + id 
              + " AND " 
              + Data.MIMETYPE 
              + " = '" 
              + Event.CONTENT_ITEM_TYPE 
              + "' AND " 
              + Event.TYPE 
              + " = " 
              + Event.TYPE_BIRTHDAY, 
            null, 
            android.provider.ContactsContract.Data.DISPLAY_NAME); 
        if (bdc.getCount() > 0) { 
         while (bdc.moveToNext()) { 
          birthDate = bdc.getString(0); 
         } 
        } 

        if (bdc != null) 
        bdc.close(); 

       Cursor ann = cr 
         .query(android.provider.ContactsContract.Data.CONTENT_URI, 
           new String[] { Event.DATA }, 
           android.provider.ContactsContract.Data.CONTACT_ID 
             + " = " 
             + id 
             + " AND " 
             + Data.MIMETYPE 
             + " = '" 
             + Event.CONTENT_ITEM_TYPE 
             + "' AND " 
             + Event.TYPE 
             + " = " 
             + Event.TYPE_ANNIVERSARY, 
           null, 
           android.provider.ContactsContract.Data.DISPLAY_NAME); 
       if (ann.getCount() > 0) { 
        while (ann.moveToNext()) { 
         anniversaryDate = ann.getString(0); 
        } 
       } 
       if (ann != null) 
        ann.close(); 


       Cursor cur1 = cr.query(
         ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Email.CONTACT_ID 
           + " = ?", new String[] { id }, null); 

       if (cur1.moveToNext()) { 
        email1 = cur1 
          .getString(cur1 
            .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
        if (email1 == null) 
         email1 = ""; 
    if (cur1 != null) 
      cur1.close(); 

if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
            null, 
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
              + " = ?", new String[] { id }, 
            null); 
        if (pCur.moveToNext()) { 

         phone = pCur 
           .getString(pCur 
             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         if (phone == null) 
          phone = ""; 
        } 

어떻게 사람이 내 코드에서 잘못하고있는 무슨 나를 인도 할 수 내 위의 코드를보다 빠르고 실행 향상시킬 수 있습니까??

답변

0

결과를 얻은 후에 UI에 표시하려면 Handler으로 지정하십시오. 그것은 당신의 코드를 더 빠르게 만들지는 않겠지 만, 적어도 그것은 당신의 UI를 고착시키지 않을 것입니다.

관련 문제