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

作为一名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

管理联系人电话的Uri

ContactsContract.CommonDataKinds.Phone.CONTENT_URI

有兴趣了解更多的朋友研究一下推荐链接

   Demo下载

(如对本文有异议的大牛请留言)

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值