几种查找电话号码效率比较
第一种:N次查找操作,效率低下。
List<TxrjContact> contacts = new ArrayList<TxrjContact>();
// 从Contacts表中找出所有联系人。
Cursor cursor = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
new String[] {"_id","display_name","has_phone_number","sort_key"},
null, null, "sort_key COLLATE LOCALIZED ASC");
if (cursor != null) {
while(cursor.moveToNext()) {
TxrjContact contact = new TxrjContact();
contact.setName(cursor.getString(cursor.getColumnIndex("display_name")));
contact.setHasNumber(cursor.getInt(cursor.getColumnIndex("has_phone_number")));
contact.setContactId(cursor.getInt(cursor.getColumnIndex("_id")));
contact.setSortKey(cursor.getString(cursor.getColumnIndex("sort_key")));
contacts.add(contact);
}
cursor.close();
}
// 从data表中找出电话号码
for(TxrjContact contact : contacts) {
if(contact.getHasNumber() != 0) {
Cursor c = context.getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Data.CONTACT_ID, Data.RAW_CONTACT_ID,
Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(contact.getContactId())}, null);
if(c != null) {
while(c.moveToNext()) {
TxrjPhone phone = new TxrjPhone();
phone.setId(c.getInt(c.getColumnIndex(Data._ID)));
phone.setRawContactId(c.getInt(c.getColumnIndex(Data.RAW_CONTACT_ID)));
phone.setContactId(c.getInt(c.getColumnIndex(Data.CONTACT_ID)));
phone.setNumber(c.getString(c.getColumnIndex(Phone.NUMBER)));
phone.setType(c.getString(c.getColumnIndex(Phone.TYPE)));
phone.setLabel(c.getString(c.getColumnIndex(Phone.LABEL)));
contact.getPhoneList().add(phone);
}
c.close();
}
}
}
第二种:一次查找数据库保存数据到内存中。效率更高。
List<TxrjContact> contacts = new ArrayList<TxrjContact>();
// 从Contacts表中找出所有联系人。
Cursor cursor = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
new String[] {"_id","display_name","has_phone_number","sort_key"},
null, null, "sort_key COLLATE LOCALIZED ASC");
if (cursor != null) {
while(cursor.moveToNext()) {
TxrjContact contact = new TxrjContact();
contact.setName(cursor.getString(cursor.getColumnIndex("display_name")));
contact.setHasNumber(cursor.getInt(cursor.getColumnIndex("has_phone_number")));
contact.setContactId(cursor.getInt(cursor.getColumnIndex("_id")));
contact.setSortKey(cursor.getString(cursor.getColumnIndex("sort_key")));
contacts.add(contact);
}
cursor.close();
}
// 从data表中找出电话号码。一次查找出所有电话号码保存在内存中。
Cursor c = context.getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Data.CONTACT_ID, Data.RAW_CONTACT_ID,
Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
null, null);
if(c != null) {
while(c.moveToNext()) {
TxrjPhone phone = new TxrjPhone();
phone.setId(c.getInt(c.getColumnIndex(Data._ID)));
phone.setRawContactId(c.getInt(c.getColumnIndex(Data.RAW_CONTACT_ID)));
phone.setContactId(c.getInt(c.getColumnIndex(Data.CONTACT_ID)));
phone.setNumber(c.getString(c.getColumnIndex(Phone.NUMBER)));
phone.setType(c.getString(c.getColumnIndex(Phone.TYPE)));
phone.setLabel(c.getString(c.getColumnIndex(Phone.LABEL)));
// 将电话号码关联保存到contacts中。
for(TxrjContact contact : contacts) {
if(contact.getHasNumber() != 0
&& contact.getContactId() == phone.getContactId()) {
contact.getPhoneList().add(phone);
break;
}
}
}
c.close();
}
第三种:只要从data表就可以获取到完整的联系人数据
public static List<TxrjContact> getContacts(Context context) {
List<TxrjContact> contacts = new ArrayList<TxrjContact>();
// 从data表中找出电话号码。一次查找出所有电话号码保存在内存中。
Cursor c = context.getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Data.CONTACT_ID, Data.RAW_CONTACT_ID,
Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone.LABEL, "sort_key"},
Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
null, "sort_key COLLATE LOCALIZED asc");
HashMap<Integer, TxrjContact> contactMap = new HashMap<Integer, TxrjContact>();
if(c != null) {
while(c.moveToNext()) {
int contactId = c.getInt(c.getColumnIndex(Data.CONTACT_ID));
TxrjContact contact = null;
if(contactMap.containsKey(contactId)) {
contact = contactMap.get(contactId);
} else {
contact = new TxrjContact();
contact.setContactId(contactId);
contact.setName(c.getString(c.getColumnIndex(Phone.DISPLAY_NAME)));
contact.setSortKey(c.getString(c.getColumnIndex("sort_key")));
contactMap.put(contactId, contact);
contacts.add(contact);
}
TxrjPhone phone = new TxrjPhone();
phone.setId(c.getInt(c.getColumnIndex(Data._ID)));
phone.setRawContactId(c.getInt(c.getColumnIndex(Data.RAW_CONTACT_ID)));
phone.setContactId(c.getInt(c.getColumnIndex(Data.CONTACT_ID)));
phone.setNumber(c.getString(c.getColumnIndex(Phone.NUMBER)));
phone.setType(c.getString(c.getColumnIndex(Phone.TYPE)));
phone.setLabel(c.getString(c.getColumnIndex(Phone.LABEL)));
contact.getPhoneList().add(phone);
}
c.close();
}
return contacts;
}