最近有個關于通訊錄開發的需求,需求很簡單:根據手機号碼查詢姓名。之前有擷取通訊錄清單的代碼如下:
/**
* 擷取本機手機聯系人清單
*
* @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位手機号碼,正确顯示該号碼對應的聯系人姓名。