Android address book read optimization Acceleration
1. When reading the address book at a time, try to read as few attributes as possible, especially when the list is displayed, it will make the loading speed of your list unbearable. We recommend that you load a few attributes first, then, all attributes are loaded at the time of details.
2. When reading a type of attribute, we recommend that you use a cursor and place it outside the loop to accelerate the speed. projection is used to indicate the columns to be queried, CONTACTOR_ION in the following code ).
The sample code is as follows:
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. Check whether the DEPARTMENT attribute of a contact is ORGANIZATION. TITLE, rather than ORGANZITION. DEPARTMENT. This is a pitfall.