方式一:通过观察导出的联系人数据库的相关字段,根据字段去查询(在有些手机上会查询不全,并且该类没有实现排序)

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;import com.wjustudio.phoneManager.Common.AppConstants;
import com.wjustudio.phoneManager.javaBean.Contact;import java.util.ArrayList;/*** 作者:songwenju on 2016/4/2 22:00* 邮箱:songwenju01@163.com*/
public class ContactUtil {/*** 获得系统联系人** @param context* @return*/public static ArrayList<Contact> getContact(Context context) {ArrayList<Contact> contacts = new ArrayList<>();Contact contact;String[] projection = {"data1", "mimetype"};Cursor rawCursor = context.getContentResolver().query(Uri.parse(AppConstants.RAW_CONTACTS),new String[]{"contact_id"}, null, null, null);if (rawCursor != null && rawCursor.getCount() > 0) {LogUtil.i("ContactUtil", "the number of contact:" + rawCursor.getCount());while (rawCursor.moveToNext()) {String contact_id = rawCursor.getString(rawCursor.getColumnIndex("contact_id"));LogUtil.i("contactUtil","contact_id:"+contact_id);if (contact_id != null) {Cursor dataCursor = context.getContentResolver().query(Uri.parse(AppConstants.DATA_CONTACTS), projection,"raw_contact_id = ?", new String[]{contact_id}, null);if (dataCursor != null && dataCursor.getCount() > 0) {contact = new Contact();while (dataCursor.moveToNext()) {String data1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));data1 = data1.trim().replaceAll("-","").replaceAll(" ","");String mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));switch (mimetype) {case "vnd.android.cursor.item/name":contact.contact_name = data1;if (ChineseUtil.isChinese(data1.charAt(0))){contact.pinYin = PinYinUtil.toPinyin(data1);}else {contact.pinYin = data1;}break;case "vnd.android.cursor.item/email_v2":contact.email = data1;break;case "vnd.android.cursor.item/phone_v2":contact.contact_phoneNum = data1;break;}}contacts.add(contact);}}}}LogUtil.e("contactUtil","contactNum:"+contacts.size());return contacts;}}

对应的bean

package com.wjustudio.contactdemo;import java.util.ArrayList;
import java.util.List;/*** 作者:songwenju on 2016/4/2 22:01* 邮箱:songwenju01@163.com*/
public class Contact {public String name;public String pinYin;public List<String> telephoneNumber = new ArrayList<>();public String email;@Overridepublic String toString() {return "Contact{" +"name='" + name + '\'' +", pinYin='" + pinYin + '\'' +", telephoneNumber=" + telephoneNumber +", email='" + email + '\'' +'}';}
}

对应的AppConstants中两个字段

/*** App所用的常量* 作者:songwenju on 2016/1/31 11:39* 邮箱:songwenju01@163.com*/
public class AppConstants {public static final String RAW_CONTACTS = "content://com.android.contacts/raw_contacts";public static final String DATA_CONTACTS = "content://com.android.contacts/data";
}

方式二:通过系统提供的字段来获取数据,并实现了按拼音排序

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;import com.wjustudio.phoneManager.javaBean.Contact;import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;import java.util.ArrayList;public class ContactUtils {public static ArrayList<Contact> getContact(Context context) {ArrayList<Contact> listMembers = new ArrayList<>();Cursor cursor = null;try {Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;// 这里是获取联系人表的电话里的信息  包括:名字,联系人id,电话号码;// 然后在根据"sort-key"排序cursor = context.getContentResolver().query(uri,new String[] { "display_name", "contact_id", "data1" },null, null, "sort_key");if (cursor != null && cursor.moveToFirst()) {LogUtil.e("contactUtil", "ContactCount:" + cursor.getCount());while (cursor.moveToNext()){Contact contact = new Contact();String contact_phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));int contact_id = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));contact.contact_name = name;contact.contact_phoneNum = contact_phoneNum.replaceAll("[^0-9]", "");contact.contact_id = contact_id;contact.pinYin = getPingYin(name);LogUtil.e("contactUtils", "contact:" + contact.toString());if (name != null)listMembers.add(contact);}}} catch (Exception e) {e.printStackTrace();} finally {if (cursor != null){cursor.close();}}return listMembers;}/*** 将字符串中的中文转化为拼音,其他字符不变** @param inputString 输入的中文汉字* @return 获得的拼音*/public static String getPingYin(String inputString) {if (TextUtils.isEmpty(inputString)) {return "";}HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.LOWERCASE);format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);format.setVCharType(HanyuPinyinVCharType.WITH_V);char[] input = inputString.trim().toCharArray();String output = "";try {for (char anInput : input) {if (Character.toString(anInput).matches("[\\u4E00-\\u9FA5]+")) {String[] temp = PinyinHelper.toHanyuPinyinStringArray(anInput, format);if (temp == null || TextUtils.isEmpty(temp[0])) {continue;}output += temp[0].replaceFirst(temp[0].substring(0, 1),temp[0].substring(0, 1).toUpperCase());} elseoutput += Character.toString(anInput);}} catch (Exception e) {e.printStackTrace();}return output;}}

对应的bean

/*** author:songwenju on 16-4-7 11 : 07* Email: songwenju01@163.com*/
public class Contact {public String contact_name;public String contact_phoneNum;public int contact_id;public String email;//public List<String> telephoneNumber = new ArrayList<>();public String pinYin;@Overridepublic String toString() {return "Contact{" +"contact_name='" + contact_name + '\'' +", contact_phoneNum='" + contact_phoneNum + '\'' +", contact_id=" + contact_id +", email='" + email + '\'' +", pinYin='" + pinYin + '\'' +'}';}}

联系人数据库里面联系人和电话号码是分别存在两个表里面的,因为存在一个联系人拥有几个号码的情况,所以android为联系人和手机号码分别单独创建了相应的视图。

联系人信息的视图里面只保存与联系人相关的资料,例如姓名,是否有手机号码等。

而手机号码资料则是每一个电话号码为一条记录,如果有一个联系人有3个号码,则里面会出现3个该联系人的记录,号码分别为他的三个号码。

如果是需要读取联系人信息,传入的URI为:ContactsContract.Contacts.CONTENT_URI

如果是需要读取手机号码信息传入的URI为:ContactsContract.CommonDataKinds.Phone.CONTENT_URI

下篇文章将写一个完整的小项目去实现读联系人,并且以自定义View的形式展示出来。

Android 获得联系人并排序相关推荐

  1. android 通讯录字母排序,Android仿微信联系人字母排序效果

    本文实例为大家分享了Android联系人字母排序的具体代码,供大家参考,具体内容如下 实现思路:首先说下布局,整个是一个相对布局,最下面是一个listview,listview上面是一个自定义的vie ...

  2. android 联系人 中文 排序,Android中文联系人排序及检索补丁的原理

    Android中文联系人排序及检索补丁的原理(090819更新) 2009年4月26日,更新了源码和相关文件 很久以前做了这个补丁,有幸的是朋友们都还算喜爱它,没白费功夫.不少朋友来信问它的原理,现在 ...

  3. Android中文联系人排序及检索补丁的原理(090819更新)

    原文转自孙志岗老师Sunner的博客:http://blog.sunner.cn/2009/04/android_pinyin_sorting/ 文章写的是针对Android1.0, 1.1及1.5( ...

  4. Android系统联系人全特效实现(下),字母表快速滚动

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9050671 在上一篇文章中,我和大家一起实现了类似于Android系统联系人的分组 ...

  5. Android系统联系人全特效实现(上),分组导航和挤压动画

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9033553 记得在我刚接触Android的时候对系统联系人中的特效很感兴趣,它会根 ...

  6. Android手机联系人URI总结

    Android手机联系人URI总结 参考来自:https://wenku.baidu.com/view/d28a2e6b2d3f5727a5e9856a561252d380eb20b2.html An ...

  7. Android contacts 联系人 通讯录 源码 完全解析

    Android contacts 联系人 通讯录 源码 完全解析 1简介 2软件架构 3各功能模块分析 1联系人数据的显示 1联系人列表显示 2联系人详细信息数据的显示 2联系人数据的编辑和存储 1编 ...

  8. Android读取联系人的姓名及电话号码

    Android中联系人的信息是通过ContentProvider来供外部应用获取的,我们使用时只需根据系统联系人ContentProvider的Uri即可获取所需数据.下面讲解如何获取联系人的姓名及电 ...

  9. Android VCard联系人备份恢复(导入/导出)详解

    原文地址为: Android VCard联系人备份恢复(导入/导出)详解 首先我们简单的看下在Android中联系人的存储结构. 工作环境:android 2.3.3 联系人的主要数据存放在raw_c ...

最新文章

  1. 中国游戏中心让计算机死机,win10玩游戏不定时死机
  2. 《Android 开发入门与实战(第二版)》——6.6节配置改变
  3. @Quelifier的用法,
  4. IIS日志自动删除程序 收藏
  5. [USACO15JAN]草鉴定Grass Cownoisseur
  6. Centos中Redis的下载编译与安装(超详细)
  7. ICCV 2021 | G-SFDA:无需源数据的领域自适应方法
  8. html 按钮防止多次提交,HTML点击提交按钮两次
  9. Angularjs基础(三)
  10. 引入Vant-UI全部组件的代码 - (备份)
  11. 阿里CEO张勇:建立良好的消费环境是阿里20年来努力工作的方向
  12. 细数微软 Teams 的 14 宗“罪”!
  13. python 获取当前路径_Python获取当前路径实现代码
  14. ps和matlab哪个,Matlab与photoshop在数字图像处理中的比较
  15. ubuntu——sudo权限问题(sudo:/etc/sudoers is world writable sudo:no valid sudoers sources found,quitting..)
  16. 设计,让交叉口更安全
  17. 应广单片机开发调试应注意的问题
  18. opencv3/C++ 将图片转换为视频
  19. 如何在Vivado创建一个FIFO的IP核并使用ILA工具验证
  20. 操作系统课程设计--在Linux环境下模拟实现简单命令解释器(C++代码)

热门文章

  1. python知网查重_学长学姐使用知网查重的经验之谈
  2. libjpeg库和libpng库的移植和使用
  3. 二、MySQL操作数据库
  4. 官网下载python,下载pycharm
  5. 前端json格式的介绍,转换以及解析
  6. 智能合约部署Error: exceeds block gas limit undefined
  7. Filter过滤器(超详细)
  8. 判断两个区间有无交集
  9. AWS KVS(Kinesis Video Streams)之WebRTC的C库测试
  10. 中国科学院计算机所张浩,航天科技集团调研组到计算所交流