android 快速查询通讯录
原创博文:http://blog.csdn.net/marchlqq/article/details/41944449
本人不是很爱写什么博客,因为自己的文笔真不怎么好。最近在开发项目中,要查询联系人,然后上网搜索了下,找到了很多篇关系查询联系人的代码,发现都是大同小异。然后也使用了,只是感觉速度很慢。也没发现有什么更快一点的,都是转来转去的文章,很是郁闷。感觉有必要写下。
这是一篇android 联系人读取的博客,随便找的,因为文笔不好,让大家可以借鉴。
http://www.cnblogs.com/jxgxy/archive/2012/07/28/2613069.html
以下是联系人查询的主要代码(copy):
//查询所有联系人的姓名,电话,邮箱 public void TestContact() throws Exception { Uri uri = Uri.parse("content://com.android.contacts/contacts"); ContentResolver resolver = getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null); while (cursor.moveToNext()) { int contractID = cursor.getInt(0); StringBuilder sb = new StringBuilder("contractID="); sb.append(contractID); uri = Uri.parse("content://com.android.contacts/contacts/" + contractID + "/data"); Cursor cursor1 = resolver.query(uri, new String[]{"mimetype", "data1", "data2"}, null, null, null); while (cursor1.moveToNext()) { String data1 = cursor1.getString(cursor1.getColumnIndex("data1")); String mimeType = cursor1.getString(cursor1.getColumnIndex("mimetype")); if ("vnd.android.cursor.item/name".equals(mimeType)) { //是姓名 sb.append(",name=" + data1); } else if ("vnd.android.cursor.item/email_v2".equals(mimeType)) { //邮箱 sb.append(",email=" + data1); } else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { //手机 sb.append(",phone=" + data1); } } cursor1.close(); Log.i(TAG, sb.toString()); } cursor.close(); }
很多转载的朋友,都是直接就使用了,都没有仔细去看这一份代码。实际上这一份代码,在联系人数据很多的时候,是很浪费时间的。
代码解读:
这份代码,是查询2个表,一个联系人表,一个手机和联系人表。
eg:手机上有3个联系人 张3 (12341234111 12341234222) 李四 (14714714711) 王五 (12312312311)
代码里面的cursor 查询到联系人姓名,张三 id = 103,李四 id = 105,王五 id = 208
代码里面的cursor1 则是通过id,查询对应的手机号码。循环遍历联系人,取得id = 103,然后,查询到手机号码 12341234111 12341234222,以此类推。
大家都应该知道一件事情,就是数据库的操作永远都是比较消耗时间的。也就是说,可以的话,尽量减少数据库的查询。但是这一段代码,是循环遍历数据库,如果有100个联系人,就要查询100次数据库,这个时间,就会很明显了。
通过这,大家应该很容易想到解决方法了。只需要减少数据库的操作就可以解决了。
以下是查询2个库的操作:
// 查询所有联系人的姓名,电话 public void queryContacts() throws Exception { ContentResolver contentResolver = mContext.getContentResolver();//首先查询联系人 Cursor cursor = contentResolver.query( ContactsContract.Contacts.CONTENT_URI, null, null, null, "sort_key ASC"); List list = new ArrayList<>(); List list2 = new ArrayList<>(); while (cursor.moveToNext()) { int nameIndex = cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); String userName = cursor.getString(nameIndex); // 取得联系人ID String contact_id = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); String pinyin = cursor .getString(cursor .getColumnIndex(ContactsContract.Contacts.SORT_KEY_PRIMARY)); Contact contact = new Contact(); contact.name = userName; contact.id = contact_id; contact.pinyin = pinyin.toLowerCase(); list.add(contact); } cursor.close();//然后查询联系人和手机号表 Cursor phone = contentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (phone.moveToNext()) { String id = phone .getString(phone .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); String strPhoneNumber = phone .getString(phone .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); strPhoneNumber = getNumber(strPhoneNumber); if (isUserNumber(strPhoneNumber)) { Contact contact = new Contact(); contact.number = strPhoneNumber; contact.id = id; list2.add(contact); } } phone.close();// 最后循环遍历2个表 for (int i = 0; i < list.size(); i++) { Contact contact = list.get(i); String str = ""; for (int j = 0; j < list2.size(); j++) { Contact contact2 = list2.get(j); if (contact.id.equals(contact2.id)) { ContactInfo contactInfo = new ContactInfo(); if (!isContainUserPhoneNumber(mContactList, contact2.number)) { if (contact.name != null) { contactInfo.setContactName(contact.name); contactInfo.setPinyin(contact.getPinyin()); } contactInfo.setUserNumber(contact2.number); mContactList.add(contactInfo); str = str + String.valueOf(j); } } } for (int k = 0; k < str.length(); k++) { list2.remove(Integer.parseInt(str.charAt(k) + "")); } } list = null; list2 = null; } class Contact { private String id; private String name; private String number; private String pinyin; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getPinyin() { return pinyin; } public void setPinyin(String pinyin) { this.pinyin = pinyin; } }
代码中的一些小方法,读者自己过滤掉吧。
经过测试,在魅族MX手机上测试结果,150左右个联系人,第一种方法消耗时间平均 2045ms,第二种平均 200ms。
希望对大家有所帮助。
更多相关文章
- 手把手教你Android来去电通话自动录音的方法
- Android(安卓)exoplayer播放在线视频教程
- Android将使用OpenJDK
- Android(安卓)MVC框架模式的理解
- Android中联系人和通话记录详解(2)
- Android(安卓)基于Bmob云的商品查询显示实战案例
- Android通过反射获取build.prop中key对应的value
- Android(安卓)桌面组件【widget】初探
- Android(安卓)Zip文件解压缩代码