我正在制作一个应用程序,我将从 sim 中获取用户的完整电话号码 - 包括国家和移动前缀。我的电话号码是 061555555,所以当我将它保存到服务器时,它应该是这样的:+38761555555。
这是我的问题 - 当我使用下面的代码时,我得到以下信息:+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;
}