您的位置:首页 > 移动开发 > Android开发

Android中获取手机联系人的优化

2015-05-15 18:14 309 查看

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.说明

获取联系人姓名和电话号码只要查询一遍数据库,参考代码             http://www.icodelogic.com/?p=453   经测试,100条联系人之前查询用时2s,优化后用时0s.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: