您的位置:首页 > 移动开发 > Android开发

最快速读取手机通讯录中联系人信息

2016-07-01 16:22 441 查看
作为一名Android开发,读写手机通讯录的操作人人都会,但是有没有遇到通讯录存在好几百条联系人信息时候读取的速度会明显变慢呢?本文就是介绍解决办法,我总结出了以下几种办法 提供参考:
一、线程
有A、B两个Activity,想在B里面显示手机通讯录中所有联系人信息,可以尝试在A的时候就开起一个单独的异步线程,读取手机通讯录,然后再带到B Activity,这种办法呢标不治本,所以不推荐
二、service
开启一个service来读取通讯录,同样需要在显示的Activity之前开启,这种办法同样治标不治本,所以不推荐
三、分页读取
Demo下载
这个方法其实很好用,但是可能满足不了多数人的需求
原理就是利用数据库的limit函数实现分页查询private void getContact(List<ContactBean> cdList, int first, int max) {
ContentResolver cr = this.getContentResolver();
Cursor resultCursor = cr.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null,
"_id limit " + first + "," + max);
if (resultCursor != null && resultCursor.getCount() != 0) {
while (resultCursor.moveToNext()) {
ContactBean cd = new ContactBean();
cd.name = resultCursor.getString(0);
cd.phoneNum = resultCursor.getString(1);
if (ispull) {
cdbfList.add(cd);
} else {
cdList.add(cd);
}
}
if (!ispull) {
currentCount += resultCursor.getCount();
}
} else {
isnull = true;}四、本文的推荐方法
先看看下面两段代码
一、
/**
* 获取系统联系人信息
* @return
*/
public List<ContactInfo> getSystemContactInfos(){
List<ContactInfo> infos=new ArrayList<ContactInfo>();

// 使用ContentResolver查找联系人数据
Cursor cursor = mContext.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);

// 遍历查询结果,获取系统中所有联系人
while (cursor.moveToNext())
{
ContactInfo info=new ContactInfo();
// 获取联系人ID
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
// 获取联系人的名字
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
info.setContactName(name);

// 使用ContentResolver查找联系人的电话号码
Cursor phones = mContext.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);

// 遍历查询结果,获取该联系人的多个电话号码
while (phones.moveToNext())
{
// 获取查询结果中电话号码列中数据。
String phoneNumber = phones.getString(phones
.getColumnIndex(ContactsContract
.CommonDataKinds.Phone.NUMBER));
info.setPhoneNumber(phoneNumber);
}
phones.close();

infos.add(info);
info=null;
}
cursor.close();

return infos;

}二、
/**
* 获取系统联系人信息
*
* @return
*/
public List<ContactsBean> getSystemContactInfos() {

List<ContactsBean> infos = new ArrayList<ContactsBean>();
Cursor cursor = mContext.getContentResolver().query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null);
if (cursor != null) {

while (cursor.moveToNext()) {

ContactsBean info = new ContactsBean();
String contactName = cursor.getString(0);
String phoneNumber = cursor.getString(1);
info.setContactName(contactName);
info.setPhoneNumber(phoneNumber);
infos.add(info);
info = null;
}
cursor.close();

}
return infos;
}
两种方法都是获取通讯录且可用,但是两者的速度却是差距很大,亲测130+联系人读取遍历到显示---方法一耗时:5秒、方法二耗时:42毫秒
分析:第一个方法一的用了多个游标 方法体内的方法繁琐,
    第二个方法相对简单多,只使用了一个游标就完成,
原因:有两个一个是游标 一个是Uri,两个方法用的Uri是不同的,而且第一个方法读取联系人时游标的count是错误的
    contactscontract.contacts.content_uri和phone.content_uri的区别
管理联系人的Uri
ContactsContract.Contacts.CONTENT_URI管理联系人电话的UriContactsContract.CommonDataKinds.Phone.CONTENT_URI
有兴趣了解更多的朋友研究一下推荐链接
   Demo下载
(如对本文有异议的大牛请留言)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息