最近有个项目,需要读取Android系统的联系人。安卓读取操作其实是联系人数据库的增删改查;IOS方面直接可以获取联系人集合,其实就是对象关系映射。
于是在网络上狂搜博客,网络上最常用读取联系人代码如下(1000联系人耗时20s),原理是先查询出联系人的contactId,name,再去查询手机号,可以看到Cursor游标里面还嵌套着Cursor,这样不耗时都难:
//获取系统联系人
public void getPhoneContacts() {
List<Contacts> data = new ArrayList<>();
ContentResolver resolver = context.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,
new String[]{android.provider.ContactsContract.Contacts._ID, android.provider.ContactsContract.Contacts.DISPLAY_NAME}, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
Contacts contacts = new Contacts();
//获取联系人的ID
String contactId = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.Contacts._ID));
//获取联系人的姓名
String name = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contacts.setRecordId(Integer.valueOf(contactId));
contacts.setName(name);
ArrayList<String> phone = new ArrayList<>();
//查询电话类型的数据操作
Cursor phones = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
if (phones != null) {
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
String replace = phoneNumber.replace(" ", "").replace("-", "").replace("+", "");
if (CheckUtils.checkPhoneNumber(replace)) {
phone.add(replace);
}
}
phones.close();
}
String[] strings = new String[phone.size()];
for (int i = 0; i < phone.size(); i++) {
strings[i] = phone.get(i);
}
contacts.setMobile(strings);
data.add(contacts);
}
phoneCursor.close();
syncAvatars(data);
}
}
所以获取联系人数据最好在一个Cursor里获取,避免嵌套,查看了联系人数据库表结构,以下的读取联系人和号码的操作(1000个联系人只需要0.2S左右)
//获取系统联系人,获取1000个联系人0.2秒,最快速
public void getPhoneContacts() {
//联系人集合
List<Contacts> data = new ArrayList<>();
ContentResolver resolver = context.getContentResolver();
//搜索字段
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME};
// 获取手机联系人
Cursor contactsCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, null, null, null);
if (contactsCursor != null) {
//key: contactId,value: 该contactId在联系人集合data的index
Map<Integer, Integer> contactIdMap = new HashMap<>();
while (contactsCursor.moveToNext()) {
//获取联系人的ID
int contactId = contactsCursor.getInt(0);
//获取联系人的姓名
String name = contactsCursor.getString(2);
//获取联系人的号码
String phoneNumber = contactsCursor.getString(1);
//号码处理
String replace = phoneNumber.replace(" ", "").replace("-", "").replace("+", "");
//判断号码是否符合手机号
if (CheckUtils.checkPhoneNumber(replace)) {
//如果联系人Map已经包含该contactId
if (contactIdMap.containsKey(contactId)) {
//得到该contactId在data的index
Integer index = contactIdMap.get(contactId);
//重新设置号码数组
Contacts contacts = data.get(index);
String[] mobile = contacts.getMobile();
String[] mobileCopy = new String[mobile.length + 1];
for (int i = 0; i < mobile.length; i++) {
mobileCopy[i] = mobile[i];
}
mobileCopy[mobileCopy.length - 1] = replace;
contacts.setMobile(mobileCopy);
} else {
//如果联系人Map不包含该contactId
Contacts contacts = new Contacts();
contacts.setRecordId(contactId);
contacts.setName(name);
String[] strings = new String[1];
strings[0] = replace;
contacts.setMobile(strings);
data.add(contacts);
contactIdMap.put(contactId, data.size() - 1);
}
}
}
contactsCursor.close();
syncAvatars(data);
}
}