1.优化方案
Map<String, String> temp = new HashMap<String, String>();
String[] phoneNumberProjection = new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME};
Cursor c = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
phoneNumberProjection, null, null, null);
if (c != null) {
long startTime = System.currentTimeMillis();
if (c.moveToFirst()) {
do {
String contactId = c.getString(0);
String name = c.getString(2);
String number = c.getString(1);
System.out.print(contactId + ":" + name + "\n");
temp.put(name, number);
} while (c.moveToNext());
}
c.close();
long endTime = System.currentTimeMillis();
long time = endTime - startTime;
System.out.println((float) (time / 1000) + "秒");
}
return temp;
2.说明
获取联系人姓名和电话号码只要查询一遍数据库,参考代码
经测试,100条联系人之前查询用时2s,优化后用时0s.