android通讯录根据手机号码查询姓名

news/2024/10/18 8:36:27/
最近有个关于通讯录开发的需求,需求很简单:根据手机号码查询姓名。之前有获取通讯录列表的代码如下:
/*** 获取本机手机联系人列表* * @author yinbiao* @date 2016-4-5 上午11:03:48* @param context* @return*/public synchronized static List<MocamContact> getLocalPhoneContacts(Context context) {String[] projection = { Phone.DISPLAY_NAME, Phone.NUMBER };List<MocamContact> list = new ArrayList<MocamContact>();ContentResolver resolver = context.getContentResolver();// 获取手机联系人Cursor cursor = resolver.query(Phone.CONTENT_URI, projection, null, null, null);if (cursor != null) {while (cursor.moveToNext()) {// 得到手机号码String phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));// 如果不是正確的手機號碼 跳过当前循环if (!isMobileNomber(phoneNumber)) {continue;}// 得到联系人名称String name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));MocamContact contact = new MocamContact(phoneNumber, name);list.add(contact);}cursor.close();}return list;}
/*** 判断是否是正确的手机号码* * @author yinbiao* @date 2016-4-6 下午3:17:17* @param mobileNumber* @return*/public static boolean isMobileNomber(String mobileNumber) {if (TextUtils.isEmpty(mobileNumber)) {return false;}Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");Matcher m = p.matcher(mobileNumber);return m.matches();}


实现该需求,我只需要拿到手机号码,然后去 Phone.CONTENT_URI表查询姓名字段即可,so 代码如下:

/*** 根据手机号码查询联系人姓名* * @author yinbiao* @date 2016-4-6 上午9:29:42* @param context* @param phoneNum* @return*/public synchronized static String getDisplayNameByPhone(Context context, String phoneNum) {String displayName = null;ContentResolver resolver = context.getContentResolver();Cursor cursor = resolver.query(Phone.CONTENT_URI, projection, Phone.NUMBER + "=?",new String[]{phoneNum}, null);if (cursor != null) {while (cursor.moveToNext()) {displayName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));if (!TextUtils.isEmpty(displayName)) {break;}}}return displayName;}
是不是比较简单?但是  坑  出现了,真机调试中,根据手机号码怎么都查询不到姓名,反复检查代码没有发现问题所在,百思不得其解。

然后反其道行之,写了一个根据姓名查询手机号码的demo,代码如下:

       public synchronized static String getPhoneByName(Context context, String name) {String displayName = null;ContentResolver resolver = context.getContentResolver();Cursor cursor = resolver.query(Phone.CONTENT_URI, projection, Phone.DISPLAY_NAME + "=?",new String[]{name}, null);if (cursor != null) {while (cursor.moveToNext()) {displayName = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));if (!TextUtils.isEmpty(displayName)) {break;}}}return displayName;}
然后输入通讯录中的某一个联系人姓名进行查询,得到了手机号码显示:



仔细一看,数据库中存的手机号码中间居然有空格,终于知道了问题的原因,这下好改了,只需要查询是,给手机号码中间特定的位置插入空格就OK,查资料发现有些系统没有空格,有些系统中间加的是横线 “-”;所以将代码做如下改动:


/*** 根据手机号码查询联系人姓名* * @author yinbiao* @date 2016-4-6 上午9:29:42* @param context* @param phoneNum(传入纯数字手机号码)* @return*/public synchronized static String getDisplayNameByPhone1(Context context, String phoneNum) {String displayName = null;String phone1 = new StringBuffer(phoneNum.subSequence(0, 3)).append(" ").append(phoneNum.substring(3, 7)).append(" ").append(phoneNum.substring(7, 11)).toString();String phone2 = new StringBuffer(phoneNum.subSequence(0, 3)).append("-").append(phoneNum.substring(3, 7)).append("-").append(phoneNum.substring(7, 11)).toString();ContentResolver resolver = context.getContentResolver();Cursor cursor = resolver.query(Phone.CONTENT_URI, projection, Phone.NUMBER + " in(?,?,?)", new String[] {phoneNum, phone1, phone2 }, null);if (cursor != null) {while (cursor.moveToNext()) {displayName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));if (!TextUtils.isEmpty(displayName)) {break;}cursor.close();}}return displayName;}
再次运行,输入11位手机号码,正确显示该号码对应的联系人姓名。



http://www.ppmy.cn/news/653132.html

相关文章

奇思妙想,获取对方手机通信录

奇思妙想&#xff0c;获取对方手机通信录 最近看了点文章&#xff0c;感觉自己飘了。&#xff08;我活在自己的幻想里&#xff09;&#xff0c; 这次做个实验&#xff0c;来给大家介绍介绍一些灰黑产业的套路。代码粗糙&#xff0c;路子野。 首先来个背景&#xff0c;接着小明的…

iphone与android共享位置,苹果手机,相互始终共享位置了以后,查看不了对方的位置...

方法一&#xff1a;usb连接线,手机有线上网 1、将手机通过usb线连接到电脑&#xff0c;勾选“设置 -> 无线和网络 -> 绑定与便携热点 -> USB绑定”选项&#xff0c;电脑上会出现虚拟网卡&#xff0c;为便于分辨&#xff0c;给虚拟网卡改个名字叫:android &#xff1b;…

python手机号信息查询身份证_Python使用xpath爬虫查询身份证信息和手机号信息并写入Excel表格...

一.这个程序在网上还是有很多范例的,所以我就将大家的总结一下,然后形成自己的小程序,废话少说,上代码 import time import requests from lxml import etree # xpath模块 import pandas as pd # 写入Excel模块 # 获取身份信息 def main(): time1 = time.time() # 将要获取…

用微信怎么定位别人手机位置

在日常生活中&#xff0c;因为某种需要&#xff0c;我们偶尔也难免会有这种用微信怎么定位别人手机位置的需求&#xff0c;那么下面我就来说下用微信怎么定位别人手机位置的操作。 方法/步骤 1 用我们的手机&#xff0c;在手机的桌面上找到微信软件&#xff0c;找到后点击它并打…

免费的手机号和ip查询的api

个人测试还不错 https://api.ioser.net/

【Android从零单排系列二十七】《Android视图控件——HorizontalScrollView》

目录 前言 一 HorizontalScrollView基本介绍 二 HorizontalScrollView使用方法 三 HorizontalScrollView常见属性及方法 四 HorizontalScrollView简单案例 五 总结 前言 小伙伴们&#xff0c;在上文中我们介绍了Android视图组件ScrollView&#xff0c;本文我们继续盘点&…

Linux下搭建打印机共享服务器(支持苹果AirPrint)

一、背景 大学刚毕业没有找到工作这段时间&#xff0c;来到华清远见学习进修一下嵌入式知识。由于我的习惯&#xff0c;身边离不开打印机&#xff0c;所以就在周末买了一台二手的惠普打印机安装到了我租的房子中&#xff0c;之前一直是使用Windows的打印机共享&#xff0c;但是…

洛谷P1008 集合

题目描述 将 1,2,…,9 共 9 个数分成 3 组&#xff0c;分别组成 3 个三位数&#xff0c;且使这 3 个三位数构成 1:2:3 的比例&#xff0c;试求出所有满足条件的 3 个三位数。 输入格式 无 输出格式 若干行&#xff0c;每行 33 个数字。按照每行第 11 个数字升序排列。 分…