博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取手机联系人信息
阅读量:6950 次
发布时间:2019-06-27

本文共 2551 字,大约阅读时间需要 8 分钟。

hot3.png

public class ContactInfo{        private String name;        private String phone;                public String getName()        {                return name;        }        public void setName(String name)        {                this.name = name;        }        public String getPhone()        {                return phone;        }        public void setPhone(String phone)        {                this.phone = phone;        }        }

public class ContactInfoService{        private Context context;                public ContactInfoService(Context context)        {                this.context = context;        }                public List
getContactInfos() { List
infos = new ArrayList
(); ContactInfo info; ContentResolver contentResolver = context.getContentResolver(); //在源码的AndroidManifest里面可以看到这些uri Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri dataUri = Uri.parse("content://com.android.contacts/data"); Cursor cursor = contentResolver.query(uri, null, null, null, null); while(cursor.moveToNext()) { info = new ContactInfo(); String id = cursor.getString(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("display_name")); info.setName(name); //通过raw_contacts里面的id拿到data里面对应的数据 Cursor dataCursor = contentResolver.query(dataUri, null, "raw_contact_id = ? ", new String[] {id}, null); while(dataCursor.moveToNext()) { String type = dataCursor.getString(dataCursor.getColumnIndex("mimetype")); //根据类型,只要电话这种类型的数据 if(type.equals("vnd.android.cursor.item/phone_v2")) { String number = dataCursor.getString(dataCursor.getColumnIndex("data1"));//拿到数据 info.setPhone(number); } } dataCursor.close(); infos.add(info); info = null; } cursor.close(); return infos; }}

转载于:https://my.oschina.net/oppo4545/blog/199015

你可能感兴趣的文章
2015年度总结暨2016大致计划
查看>>
ubuntu12.04下网络爬虫 larbin 的安装
查看>>
JSP 服务器响应
查看>>
一步步制作rpm包
查看>>
App支付签名错误
查看>>
kali linux虚拟wifi搭建
查看>>
jquery设置元素的readonly和disabled
查看>>
监控文件是否被修改
查看>>
Linux学习笔记:Rsync
查看>>
转:APK Crack
查看>>
Beanstalkd协议 二(任务的生命周期)
查看>>
jvisualvm远程监控 visualgc插件 不受此jvm支持问题
查看>>
(1)Powershell简介
查看>>
zabbix客户端配置
查看>>
Logtail提升采集性能
查看>>
史上最失败的一次营销活动
查看>>
asp.net5发神经一例 ------无法加载依赖
查看>>
数据库水平切分的实现原理解析
查看>>
nova boot from volume在多主机zone下的坑
查看>>
uip中关于web服务器的简单例子
查看>>