0
我必须创建一个列表视图,其中包含手机上所有联系人的姓名和电话号码。 我的代码正在工作,但用户有很多联系时花费的时间太多。 你有想法,我可以改进它吗?改善时间获取电话号码
public List<Contact> fetchContacts() {
String phoneNumber = null;
String email = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, DISPLAY_NAME + " ASC");
List<Contact> contactList = new ArrayList<Contact>();
// Loop for every contact in the phone
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);
phoneCursor.moveToNext();
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
phoneCursor.close();
Contact contacto = new Contact();
contacto.name = name;
contacto.phone = phoneNumber;
contacto.id = contact_id;
contactList.add(contacto);
}
}
}
return contactList;
}
不要把你的问题的标题您的谷歌搜索回收快的观点把您期望从哟看到的*实际搜索结果您的问题标题中包含Google搜索*。 – 2014-09-22 19:11:28