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,這個是個坑。