android中读取通讯录学习笔记

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,这个是个坑。



原文地址:https://www.cnblogs.com/zhchoutai/p/6911555.html