1、读取通讯录时一次读取时,尽量少读取所有属性,特别是列表展示的时候,会让你的列表加载速度变得难以忍受,建议先加载少量属性,然后在详情的时候加载所有属性。
2、在读取一类属性的时候,建议用一个游标,且放在循环外面,能明显加快速度,用projection(表示需要查询的列,在下面代码中是CONTACTOR_ION)。
示例代码如下:
private static final String[] CONTACTOR_ION = new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
。。。
Cursor phones = null;
ContentResolver cr = getContentResolver();
try {
phones = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
, CONTACTOR_ION, null, null, "sort_key");
if (phones != null) {
final int contactIdIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
final int displayNameIndex = phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneString, displayNameString, contactIdString;
while (phones.moveToNext()) {
LinkManForm linkManForm = new LinkManForm();
phoneString = phones.getString(phoneIndex);
displayNameString = phones.getString(displayNameIndex);
contactIdString = phones.getString(contactIdIndex);
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
} finally {
if (phones != null)
phones.close();
}
3、查询联系人的部门属性是ORGANIZATION.TITLE,而不是ORGANZITION.DEPARTMENT,这个是个坑。