아래 소스와 같이 이메일과 전화번호를 찾아 총 세번(main, email, phone number)에 루프를 돌려 리스트에 넣어주고 있습니다.
500개정도의 주소록이 있을 때 약 50초가 소요되고 있는데, 개선 시킬 수 있는 방법이 없을까요? 경험자분 조언부탁드립니다.
public void getContactList() {
ContentResolver cr = App.context.getContentResolver();
Cursor cursor = cr.query(Contacts.CONTENT_URI, null, null, null, Contacts.SORT_KEY_PRIMARY + " ASC");
while(cursor.moveToNext()) {
ContactModel contactModel;
boolean emailsAdded = false;
String contactId = cursor.getString(cursor.getColumnIndex(Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY));
boolean bHasPhoneNumber = (Integer.parseInt(cursor.getString(
cursor.getColumnIndex(Contacts.HAS_PHONE_NUMBER))) > 0);
Cursor emails = cr.query(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
emailsAdded = true;
contactModel = new ContactModel();
contactModel.Name = contactName;
contactModel.Email = emails.getString(emails.getColumnIndex(Email.DATA));
if(UserManager.getUserInfoModel().UserEmail !=null) {
if (contactModel.Email.equals(UserManager.getUserInfoModel().UserEmail)) {
continue;
}
}
InfoModel im = infoManager.getInfoByEmail(contactModel.Email);
if(im != null) {
continue;
}
ContactList.add(contactModel);
}
emails.close();
if (!emailsAdded) {
if (bHasPhoneNumber) {
Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
if (type == Phone.TYPE_MOBILE) {
contactModel = new ContactModel();
contactModel.Name = contactName;
contactModel.Phone = phones.getString(phones.getColumnIndex(Phone.NUMBER));
ContactList.add(contactModel);
break;
}
}
phones.close();
}
}
}
cursor.close();
}