我正在製作一個應用程序,我將從SIM卡中獲取用戶的完整電話號碼 - 包括國家和移動前綴。我的電話號碼是061555555,所以當我將它保存到服務器時,它應該如下所示:+38761555555。在Android中獲取錯誤的移動前綴
這裏是我的問題 - 當我使用下面的代碼時,我得到以下內容:+387218900032555555,即。 而不是061變成61,它變成218900032。該行號給出了錯誤的號碼:
MyPhoneNumber = tMgr.getLine1Number();
這是我的代碼:
// Get users phone number
private String getMyPhoneNumber() {
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// Get the SIM country ISO code
String simCountry = tMgr.getSimCountryIso().toUpperCase();
// Get the operator code of the active SIM (MCC + MNC)
String simOperatorCode = tMgr.getSimOperator();
// Get the name of the SIM operator
String simOperatorName = tMgr.getSimOperatorName();
// Get the SIM’s serial number
String simSerial = tMgr.getSimSerialNumber();
String MyPhoneNumber = "0000000000";
try {
MyPhoneNumber = tMgr.getLine1Number();
} catch (NullPointerException ex) {
}
if (MyPhoneNumber.equals("")) {
MyPhoneNumber = tMgr.getSubscriberId();
}
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber countryNumberProto = null;
try {
countryNumberProto = phoneUtil.parse(MyPhoneNumber, simCountry);
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
String myPhone = phoneUtil.format(countryNumberProto, PhoneNumberUtil.PhoneNumberFormat.E164);
// I tried INTERNATIONAL and NATIONAL as well
return myPhone;
}
當我調試我得到的是以下內容:'國家代碼:387國內號碼21890032555555'。 – Kemo
這表明38不是「simCountry」的國家代碼,並且其中一個與你的Q相同 – VVB
我的國家代碼是387,但我的國內號碼(移動前綴)應該以61而不是21890032開頭。對不起,如果我做不正確地理解你的答案,我再次更新了我的代碼。你能指出我的代碼錯誤嗎? – Kemo