如代码所示,2的速度明显快好几倍
public void getContact(Context con){ Cursor cursor=con.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION,null,null, QUERY_ORDER_BY_NAME); if (cursor != null) { try { List<UContact> uContacts = new ArrayList<UContact>(); if (cursor.moveToFirst()) { do { UContact item = new UContact(); item.setId(cursor.getLong(0)); item.setPhotoId(cursor.getLong(1)); item.setName(cursor.getString(2)); item.setFirstChar(getFirstChar(cursor .getString(android.os.Build.VERSION.SDK_INT >= 8 ? 3 : 2))); if (android.os.Build.VERSION.SDK_INT >= 8) item.setSortKey(cursor.getString(3)); queryUMGroupById(con,item, item.getId()); uContacts.add(item); } while (cursor.moveToNext()); } Collections.sort(uContacts, new ArrayCompare()); isContactChange = false; // mContactCount.put(0, uContacts.size()); } catch (Exception e) { e.printStackTrace(); } finally { cursor.close(); cursor = null; } } } private void queryUMGroupById(Context con,UContact contact, long id) { int groupId=0; Cursor cursor=con.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { CommonDataKinds.GroupMembership.GROUP_ROW_ID } , Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + " =?", new String[] { String.valueOf(id), UM_GROUP_MIMETYPE }, null); if(cursor!=null){ int count=cursor.getCount(); if(count>0){ cursor.moveToFirst(); groupId= cursor.getInt(0); contact.setUmGroupId(groupId); if(cursor!=null){ cursor.close(); cursor=null; } } } } public void getContact2(Context con){ Cursor cursor_group=con.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {Data.CONTACT_ID,CommonDataKinds.GroupMembership.GROUP_ROW_ID } , Data.MIMETYPE + " =?", new String[] {UM_GROUP_MIMETYPE }, null); if(cursor_group!=null){ mGroupMap.clear(); int count=cursor_group.getCount(); if(count>0){ cursor_group.moveToFirst(); for(int i=0;i<count;i++){ int contactId=cursor_group.getInt(0); int groupId=cursor_group.getInt(1); mGroupMap.put(contactId, groupId); cursor_group.moveToNext(); } if(cursor_group!=null){ cursor_group.close(); cursor_group=null; } } } Cursor cursor=con.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION,null,null, QUERY_ORDER_BY_NAME); if (cursor != null) { try { List<UContact> uContacts = new ArrayList<UContact>(); if (cursor.moveToFirst()) { do { UContact item = new UContact(); item.setId(cursor.getLong(0)); item.setPhotoId(cursor.getLong(1)); item.setName(cursor.getString(2)); item.setFirstChar(getFirstChar(cursor .getString(android.os.Build.VERSION.SDK_INT >= 8 ? 3 : 2))); if (android.os.Build.VERSION.SDK_INT >= 8) item.setSortKey(cursor.getString(3)); item.setUmGroupId(mGroupMap.get(item.getId())); uContacts.add(item); } while (cursor.moveToNext()); } Collections.sort(uContacts, new ArrayCompare()); isContactChange = false; // mContactCount.put(0, uContacts.size()); } catch (Exception e) { e.printStackTrace(); } finally { cursor.close(); cursor = null; } } }