2014-09-26 46 views
2

這應該是一個常見問題,但我不知道尋找解決方案的正確詞語是什麼。由於格式差異無法找到聯繫人號碼

在我的應用程序中,用戶可以鍵入一個數字,並且我需要搜索聯繫人是否存在於數據庫中。此外,給定聯繫人ID和號碼,我想找到號碼的手機標籤..

問題是,如果我有一個電話號碼爲「8710 2863」,我的查詢參數是「87102863」,我將無法獲得聯繫人和電話標籤......必須有規範的格式的方式..但我不能找到辦法:(

這裏是我的代碼:

Cursor cursor = MyApp.getContext().getContentResolver().query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       new String[] { 
         ContactsContract.CommonDataKinds.Phone.TYPE, 
         ContactsContract.CommonDataKinds.Phone.LABEL 
       }, 
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " 
         + ContactsContract.CommonDataKinds.Phone.NUMBER + " = ? ", 
       new String[] { Long.toString(contactId), number }, 
       ContactsContract.Data.IS_SUPER_PRIMARY + " DESC"); 

     if (cursor != null) { 
      try { 
       if (cursor.moveToFirst()) { 
        int type = cursor.getInt(0); 
        String label = cursor.getString(1); 
        String phoneLabel = (String) ContactsContract.CommonDataKinds.Phone 
          .getTypeLabel(MyApp.getContext().getResources(), type, label); 
        hashBuilder.put(Defs.ARG_PHONE_TYPE, phoneLabel); 
       } 
      } finally { 
       cursor.close(); 
      } 
     } 
+1

你試了['PhoneNumberUtils.formatNumber(String)'](http://developer.andr oid.com/reference/android/telephony/PhoneNumberUtils.html#formatNumber%28java.lang.String%29)? – Barend 2014-09-26 11:00:34

+0

@勇敢的神奇。我使用PhoneNUmberUtil.format,它不如PhoneNumberUtils.formatNumber()。請留下您的答案,我會接受。 – xialin 2014-09-26 12:04:33

+0

高興地幫助:)。 – Barend 2014-09-26 13:03:29

回答