2013-04-21 38 views
1

我正在研究捕獲Android設備中的輸入/輸出SMS的應用程序。Android的Eclipse傳入與傳出的短信:電話號碼格式?

現在,因爲我已經在測試版本中,我注意到在我的SQL服務器上,傳入的SMS地址被格式化爲國際號碼,例如+32477889977。

但是,傳出的短信會像0477 88 99 77一樣被格式化,即用戶將其存儲在手機上的方式。

我對即將離任的短信代碼是

 
int addressColumn = cursor.getColumnIndex("address"); 
String number = cursor.getString(addressColumn); 

但在進入監控代碼

 
String from = msg[i].getOriginatingAddress(); 

現在的問題是,我怎麼能做出icoming短信的格式爲相同傳出,所以國際號碼,而不是存儲在手機中的號碼?

對於我的申請,電話號碼是國際性的,因爲我的目標是一個全球性的申請。所以本地格式化不是一種選擇。

我真的需要讀取網絡發送的號碼。

任何人都有這方面的經驗?我現在搜索了幾個小時的谷歌,結果。

謝謝!

回答

2

您應該使用E164數字格式。 E164是用於電話號碼格式的標準,它考慮到您正在使用的語言環境。

谷歌有自己的E164庫:https://code.google.com/p/libphonenumber/


使用該庫看起來就像在E164格式化數的例子:

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); 
    PhoneNumber phoneNumber; 
    try { 
     phoneNumber = phoneUtil.parse(phone, locale.getCountry()); 
    } catch (NumberParseException e) { 
     throw new InvalidNumberException(convertErrorType(e.getErrorType()), e.getMessage(), e); 
    } 
    String formatedMsisdn = phoneUtil.format(phoneNumber, PhoneNumberFormat.E164); 


當您收到的短信號碼已經是e164格式的o使用lib時你會得到相同的數字,但是當你發送一個短信時,你應該先使用上面的代碼格式化它以獲得它的E164格式。
您使用的語言環境是典型的設備設置爲語言環境,你可以通過調用得到它:

Locale.getDefault() 

然後傳遞區域的E164庫。

+0

我真的很喜歡你的答案,但是,我總是得到錯誤:缺少元數據:/ com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BE。你之前似乎已經使用過lib,你能幫我嗎? – 2013-04-21 13:27:58

+0

你可以顯示導致錯誤的代碼示例嗎? – 2013-04-21 13:42:55

+0

Locale local = Locale.getDefault(); \t \t \t \t \t \t String swissNumberStr = sms.mNumber; \t \t \t PhoneNumber swissNumberProto = null; \t \t \t \t \t \t \t PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); \t \t \t \t try { \t \t \t \t swissNumberProto = phoneUtil.parse(swissNumberStr, local.getCountry()); \t \t \t \t } catch (NumberParseException e) { \t \t \t \t System.err.println("NumberParseException was thrown: " + e.toString()); \t \t \t \t } \t \t \t \t \t String testing = phoneUtil.format(swissNumberProto, PhoneNumberFormat.E164);
2013-04-22 14:33:41