0

我正在制作一个应用程序,我将从 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;
}
4

2 回答 2

1

我认为您正在解析“national_number”而不是“country_code”。检查您的 JSON 解析代码字段逻辑

{
  "country_code": 41,
  "national_number": 446681800
}

https://github.com/googlei18n/libphonenumber

于 2017-02-22T10:09:29.133 回答
0

我检查了我的设置->状态,发现我的电话号码是未知的,而不是我的电话号码,它显示的是 IMEI,所以我想我会让用户通过手动输入来确认他的电话号码。

于 2017-02-24T10:52:57.333 回答