最近有个关于通讯录开发的需求,需求很简单:根据手机号码查询姓名。之前有获取通讯录列表的代码如下:
/**
* 获取本机手机联系人列表
*
* @author yinbiao
* @date 2016-4-5 上午11:03:48
* @param context
* @return
*/
public synchronized static List getLocalPhoneContacts(Context context) {
String[] projection = { Phone.DISPLAY_NAME, Phone.NUMBER };
List list = new ArrayList();
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位手机号码,正确显示该号码对应的联系人姓名。