1. 程式人生 > >android通訊錄根據手機號碼查詢姓名

android通訊錄根據手機號碼查詢姓名

最近有個關於通訊錄開發的需求,需求很簡單:根據手機號碼查詢姓名。之前有獲取通訊錄列表的程式碼如下:
/**
	 * 獲取本機手機聯絡人列表
	 * 
	 * @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位手機號碼,正確顯示該號碼對應的聯絡人姓名。