android如何获取联系人所有信息
发布时间 - 2026-01-11 02:25:57 点击率:次只要是开发和手机通讯录有关的应用,总要学会获取联系人信息,每次都google很麻烦,怎么办?

写一个工具类,获取到通讯录里所有的信息并分好类,至于大家怎么用就不管了,看下代码就都明白了,虽然代码很多,但是很简单,大部分都已分类,如果有没有写上的,大家可以打开自己手机上通讯录数据库,里面的字段都有标明,用的内容提供者,因此我们只需要拿到那个字段名基本上就能取出数据了。
工具类:
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.util.Log;
/**
*
* @author larson
*
*/
public class ContactUtil {
private List<Contacts> list;
private Context context;
private JSONObject contactData;
private JSONObject jsonObject;
public ContactUtil(Context context) {
this.context = context;
}
// ContactsContract.Contacts.CONTENT_URI= content://com.android.contacts/contacts;
// ContactsContract.Data.CONTENT_URI = content://com.android.contacts/data;
/**
* 获取联系人信息,并把数据转换成json数据
*
* @return
* @throws JSONException
*/
public String getContactInfo() throws JSONException {
list = new ArrayList<Contacts>();
contactData = new JSONObject();
String mimetype = "";
int oldrid = -1;
int contactId = -1;
// 1.查询通讯录所有联系人信息,通过id排序,我们看下android联系人的表就知道,所有的联系人的数据是由RAW_CONTACT_ID来索引开的
// 所以,先获取所有的人的RAW_CONTACT_ID
Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI,
null, null, null, Data.RAW_CONTACT_ID);
int numm = 0;
while (cursor.moveToNext()) {
contactId = cursor.getInt(cursor
.getColumnIndex(Data.RAW_CONTACT_ID));
if (oldrid != contactId) {
jsonObject = new JSONObject();
contactData.put("contact" + numm, jsonObject);
numm++;
oldrid = contactId;
}
mimetype = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE)); // 取得mimetype类型,扩展的数据都在这个类型里面
// 1.1,拿到联系人的各种名字
if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) {
cursor.getString(cursor
.getColumnIndex(StructuredName.DISPLAY_NAME));
String prefix = cursor.getString(cursor
.getColumnIndex(StructuredName.PREFIX));
jsonObject.put("prefix", prefix);
String firstName = cursor.getString(cursor
.getColumnIndex(StructuredName.FAMILY_NAME));
jsonObject.put("firstName", firstName);
String middleName = cursor.getString(cursor
.getColumnIndex(StructuredName.MIDDLE_NAME));
jsonObject.put("middleName", middleName);
String lastname = cursor.getString(cursor
.getColumnIndex(StructuredName.GIVEN_NAME));
jsonObject.put("lastname", lastname);
String suffix = cursor.getString(cursor
.getColumnIndex(StructuredName.SUFFIX));
jsonObject.put("suffix", suffix);
String phoneticFirstName = cursor.getString(cursor
.getColumnIndex(StructuredName.PHONETIC_FAMILY_NAME));
jsonObject.put("phoneticFirstName", phoneticFirstName);
String phoneticMiddleName = cursor.getString(cursor
.getColumnIndex(StructuredName.PHONETIC_MIDDLE_NAME));
jsonObject.put("phoneticMiddleName", phoneticMiddleName);
String phoneticLastName = cursor.getString(cursor
.getColumnIndex(StructuredName.PHONETIC_GIVEN_NAME));
jsonObject.put("phoneticLastName", phoneticLastName);
}
// 1.2 获取各种电话信息
if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
int phoneType = cursor
.getInt(cursor.getColumnIndex(Phone.TYPE)); // 手机
if (phoneType == Phone.TYPE_MOBILE) {
String mobile = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("mobile", mobile);
}
// 住宅电话
if (phoneType == Phone.TYPE_HOME) {
String homeNum = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("homeNum", homeNum);
}
// 单位电话
if (phoneType == Phone.TYPE_WORK) {
String jobNum = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("jobNum", jobNum);
}
// 单位传真
if (phoneType == Phone.TYPE_FAX_WORK) {
String workFax = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("workFax", workFax);
}
// 住宅传真
if (phoneType == Phone.TYPE_FAX_HOME) {
String homeFax = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("homeFax", homeFax);
} // 寻呼机
if (phoneType == Phone.TYPE_PAGER) {
String pager = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("pager", pager);
}
// 回拨号码
if (phoneType == Phone.TYPE_CALLBACK) {
String quickNum = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("quickNum", quickNum);
}
// 公司总机
if (phoneType == Phone.TYPE_COMPANY_MAIN) {
String jobTel = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("jobTel", jobTel);
}
// 车载电话
if (phoneType == Phone.TYPE_CAR) {
String carNum = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("carNum", carNum);
} // ISDN
if (phoneType == Phone.TYPE_ISDN) {
String isdn = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("isdn", isdn);
} // 总机
if (phoneType == Phone.TYPE_MAIN) {
String tel = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("tel", tel);
}
// 无线装置
if (phoneType == Phone.TYPE_RADIO) {
String wirelessDev = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("wirelessDev", wirelessDev);
} // 电报
if (phoneType == Phone.TYPE_TELEX) {
String telegram = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("telegram", telegram);
}
// TTY_TDD
if (phoneType == Phone.TYPE_TTY_TDD) {
String tty_tdd = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("tty_tdd", tty_tdd);
}
// 单位手机
if (phoneType == Phone.TYPE_WORK_MOBILE) {
String jobMobile = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("jobMobile", jobMobile);
}
// 单位寻呼机
if (phoneType == Phone.TYPE_WORK_PAGER) {
String jobPager = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("jobPager", jobPager);
} // 助理
if (phoneType == Phone.TYPE_ASSISTANT) {
String assistantNum = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("assistantNum", assistantNum);
} // 彩信
if (phoneType == Phone.TYPE_MMS) {
String mms = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
jsonObject.put("mms", mms);
}
String mobileEmail = cursor.getString(cursor
.getColumnIndex(Email.DATA));
jsonObject.put("mobileEmail", mobileEmail);
}
}
// 查找event地址
if (Event.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出时间类型
int eventType = cursor.getInt(cursor.getColumnIndex(Event.TYPE)); // 生日
if (eventType == Event.TYPE_BIRTHDAY) {
String birthday = cursor.getString(cursor
.getColumnIndex(Event.START_DATE));
jsonObject.put("birthday", birthday);
}
// 周年纪念日
if (eventType == Event.TYPE_ANNIVERSARY) {
String anniversary = cursor.getString(cursor
.getColumnIndex(Event.START_DATE));
jsonObject.put("anniversary", anniversary);
}
}
// 获取即时通讯消息
if (Im.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出即时消息类型
int protocal = cursor.getInt(cursor.getColumnIndex(Im.PROTOCOL));
if (Im.TYPE_CUSTOM == protocal) {
String workMsg = cursor.getString(cursor
.getColumnIndex(Im.DATA));
jsonObject.put("workMsg", workMsg);
} else if (Im.PROTOCOL_MSN == protocal) {
String workMsg = cursor.getString(cursor
.getColumnIndex(Im.DATA));
jsonObject.put("workMsg", workMsg);
}
if (Im.PROTOCOL_QQ == protocal) {
String instantsMsg = cursor.getString(cursor
.getColumnIndex(Im.DATA));
jsonObject.put("instantsMsg", instantsMsg);
}
}
// 获取备注信息
if (Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
String remark = cursor.getString(cursor.getColumnIndex(Note.NOTE));
jsonObject.put("remark", remark);
}
// 获取昵称信息
if (Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
String nickName = cursor.getString(cursor
.getColumnIndex(Nickname.NAME));
jsonObject.put("nickName", nickName);
}
// 获取组织信息
if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出组织类型
int orgType = cursor.getInt(cursor
.getColumnIndex(Organization.TYPE)); // 单位
if (orgType == Organization.TYPE_CUSTOM) { // if (orgType ==
// Organization.TYPE_WORK)
// {
String company = cursor.getString(cursor
.getColumnIndex(Organization.COMPANY));
jsonObject.put("company", company);
String jobTitle = cursor.getString(cursor
.getColumnIndex(Organization.TITLE));
jsonObject.put("jobTitle", jobTitle);
String department = cursor.getString(cursor
.getColumnIndex(Organization.DEPARTMENT));
jsonObject.put("department", department);
}
}
// 获取网站信息
if (Website.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出组织类型
int webType = cursor.getInt(cursor.getColumnIndex(Website.TYPE)); // 主页
if (webType == Website.TYPE_CUSTOM) {
String home = cursor.getString(cursor
.getColumnIndex(Website.URL));
jsonObject.put("home", home);
} // 主页
else if (webType == Website.TYPE_HOME) {
String home = cursor.getString(cursor
.getColumnIndex(Website.URL));
jsonObject.put("home", home);
}
// 个人主页
if (webType == Website.TYPE_HOMEPAGE) {
String homePage = cursor.getString(cursor
.getColumnIndex(Website.URL));
jsonObject.put("homePage", homePage);
}
// 工作主页
if (webType == Website.TYPE_WORK) {
String workPage = cursor.getString(cursor
.getColumnIndex(Website.URL));
jsonObject.put("workPage", workPage);
}
}
// 查找通讯地址
if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出邮件类型
int postalType = cursor.getInt(cursor
.getColumnIndex(StructuredPostal.TYPE)); // 单位通讯地址
if (postalType == StructuredPostal.TYPE_WORK) {
String street = cursor.getString(cursor
.getColumnIndex(StructuredPostal.STREET));
jsonObject.put("street", street);
String ciry = cursor.getString(cursor
.getColumnIndex(StructuredPostal.CITY));
jsonObject.put("ciry", ciry);
String box = cursor.getString(cursor
.getColumnIndex(StructuredPostal.POBOX));
jsonObject.put("box", box);
String area = cursor.getString(cursor
.getColumnIndex(StructuredPostal.NEIGHBORHOOD));
jsonObject.put("area", area);
String state = cursor.getString(cursor
.getColumnIndex(StructuredPostal.REGION));
jsonObject.put("state", state);
String zip = cursor.getString(cursor
.getColumnIndex(StructuredPostal.POSTCODE));
jsonObject.put("zip", zip);
String country = cursor.getString(cursor
.getColumnIndex(StructuredPostal.COUNTRY));
jsonObject.put("country", country);
}
// 住宅通讯地址
if (postalType == StructuredPostal.TYPE_HOME) {
String homeStreet = cursor.getString(cursor
.getColumnIndex(StructuredPostal.STREET));
jsonObject.put("homeStreet", homeStreet);
String homeCity = cursor.getString(cursor
.getColumnIndex(StructuredPostal.CITY));
jsonObject.put("homeCity", homeCity);
String homeBox = cursor.getString(cursor
.getColumnIndex(StructuredPostal.POBOX));
jsonObject.put("homeBox", homeBox);
String homeArea = cursor.getString(cursor
.getColumnIndex(StructuredPostal.NEIGHBORHOOD));
jsonObject.put("homeArea", homeArea);
String homeState = cursor.getString(cursor
.getColumnIndex(StructuredPostal.REGION));
jsonObject.put("homeState", homeState);
String homeZip = cursor.getString(cursor
.getColumnIndex(StructuredPostal.POSTCODE));
jsonObject.put("homeZip", homeZip);
String homeCountry = cursor.getString(cursor
.getColumnIndex(StructuredPostal.COUNTRY));
jsonObject.put("homeCountry", homeCountry);
}
// 其他通讯地址
if (postalType == StructuredPostal.TYPE_OTHER) {
String otherStreet = cursor.getString(cursor
.getColumnIndex(StructuredPostal.STREET));
jsonObject.put("otherStreet", otherStreet);
String otherCity = cursor.getString(cursor
.getColumnIndex(StructuredPostal.CITY));
jsonObject.put("otherCity", otherCity);
String otherBox = cursor.getString(cursor
.getColumnIndex(StructuredPostal.POBOX));
jsonObject.put("otherBox", otherBox);
String otherArea = cursor.getString(cursor
.getColumnIndex(StructuredPostal.NEIGHBORHOOD));
jsonObject.put("otherArea", otherArea);
String otherState = cursor.getString(cursor
.getColumnIndex(StructuredPostal.REGION));
jsonObject.put("otherState", otherState);
String otherZip = cursor.getString(cursor
.getColumnIndex(StructuredPostal.POSTCODE));
jsonObject.put("otherZip", otherZip);
String otherCountry = cursor.getString(cursor
.getColumnIndex(StructuredPostal.COUNTRY));
jsonObject.put("otherCountry", otherCountry);
}
}
cursor.close();
Log.i("contactData", contactData.toString());
return contactData.toString();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# android获取联系人所有信息
# android获取联系人
# android获取联系人信息
# Android ContentProvider实现手机联系人读取和插入
# Android读取手机通讯录联系人到自己项目
# android仿微信联系人索引列表功能
# Android保存联系人到通讯录的方法
# Android使用AsyncQueryHandler实现获取手机联系人功能
# Android ContentProvider实现获取手机联系人功能
# Android 获取手机联系人实例代码详解
# android实现读取、搜索联系人的代码
# Android ContentProvider获取手机联系人实例
# Android小程序实现访问联系人
# 都有
# 都在
# 就能
# 就不
# 是由
# 很简单
# 只需要
# 都已
# 每次都
# 总要
# 转换成
# 并把
# 机上
# 写上
# 即时通讯
# 就都
# 大家多多
# 很麻烦
# 管了
# 字段名
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
C++时间戳转换成日期时间的步骤和示例代码
图册素材网站设计制作软件,图册的导出方式有几种?
如何基于云服务器快速搭建网站及云盘系统?
开心动漫网站制作软件下载,十分开心动画为何停播?
如何用wdcp快速搭建高效网站?
Laravel怎么为数据库表字段添加索引以优化查询
在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?
专业商城网站制作公司有哪些,pi商城官网是哪个?
微信小程序 wx.uploadFile无法上传解决办法
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
在线制作视频的网站有哪些,电脑如何制作视频短片?
如何快速重置建站主机并恢复默认配置?
html5的keygen标签为什么废弃_替代方案说明【解答】
微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】
如何快速查询网站的真实建站时间?
如何在IIS服务器上快速部署高效网站?
如何在新浪SAE免费搭建个人博客?
Claude怎样写约束型提示词_Claude约束提示词写法【教程】
如何用已有域名快速搭建网站?
香港服务器租用费用高吗?如何避免常见误区?
怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?
怎样使用JSON进行数据交换_它有什么限制
Laravel Eloquent性能优化技巧_Laravel N+1查询问题解决
iOS验证手机号的正则表达式
深入理解Android中的xmlns:tools属性
Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置
JavaScript Ajax实现异步通信
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
太平洋网站制作公司,网络用语太平洋是什么意思?
nodejs redis 发布订阅机制封装实现方法及实例代码
免费视频制作网站,更新又快又好的免费电影网站?
浅谈redis在项目中的应用
购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?
网站制作企业,网站的banner和导航栏是指什么?
如何在IIS7中新建站点?详细步骤解析
Laravel的契約(Contracts)是什么_深入理解Laravel Contracts与依赖倒置
Laravel怎么集成Vue.js_Laravel Mix配置Vue开发环境
Python结构化数据采集_字段抽取解析【教程】
如何在建站之星网店版论坛获取技术支持?
Laravel软删除怎么实现_Laravel Eloquent SoftDeletes功能使用教程
标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南
JS去除重复并统计数量的实现方法
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
网页设计与网站制作内容,怎样注册网站?
高端企业智能建站程序:SEO优化与响应式模板定制开发
网站建设整体流程解析,建站其实很容易!
如何基于PHP生成高效IDC网络公司建站源码?
Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询
如何快速启动建站代理加盟业务?
laravel怎么用DB facade执行原生SQL查询_laravel DB facade原生SQL执行方法

