/** * 获取联系人 * @return */ public static List<ContactInfo> getContactInfos(Context context) { ContentResolver resolver = context.getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri dataUri = Uri.parse("content://com.android.contacts/data"); List<ContactInfo> contactInfos = new ArrayList<ContactInfo>(); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String id = cursor.getString(0); if (id != null) { ContactInfo info = new ContactInfo(); Cursor dataCursor = resolver.query(dataUri, new String[] { "mimetype", "data1" }, "raw_contact_id=?", new String[] { id }, null); while (dataCursor.moveToNext()) { if("vnd.android.cursor.item/name".equals( dataCursor.getString(0))){ info.setName(dataCursor.getString(1)); }else if("vnd.android.cursor.item/phone_v2".equals( dataCursor.getString(0))){ info.setPhone(dataCursor.getString(1)); } } contactInfos.add(info); dataCursor.close(); } } cursor.close(); return contactInfos; }
要使用到这三个表,但是谷歌内部在查询这个data
表的时候内部使用的并不是查询data
表而是查询了data
表的视图,这个视图中直接将mime_Type
类型给关联好了,所以这里直接查询的就是mimetype
这个类型就可以了,还有就是如果自己的手机中如果删除了一个联系人在查询的时候就会报错,这里因为是在手机中删除一个联系人的时候并不是清空数据库中的数据,而是将这个raw_contacks中的id置为null,所以会报错,这里就要进行判断一下查出来的id是否为null。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!