0

I am making an app where I will get user's complete phone number from the sim - including the country and mobile prefixes. The phone number I have is 061555555, so when I save it to server it should look like this: +38761555555.

And here is my problem - when I use the code from below I get the following: +387218900032555555, ie. instead of 061 becoming 61, it becomes 218900032. This line number gives the wrong number:

MyPhoneNumber = tMgr.getLine1Number(); 

I have also tried this and this.

This is my code:

// 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;
}
Banana
  • 2,435
  • 7
  • 34
  • 60

2 Answers2

1

I think you are parsing "national_number" instead of "country_code". Check your JSON parsing code fields logic

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

https://github.com/googlei18n/libphonenumber

VVB
  • 7,363
  • 7
  • 49
  • 83
  • When I debug what I get is the following: `Country Code: 387 National Number 21890032555555`. – Banana Feb 22 '17 at 10:36
  • It indicates 38 is not the country code for "simCountry" & above one is as equal as your Q. – VVB Feb 22 '17 at 10:40
  • My country code is 387, but my National number (mobile prefix) should start with 61 instead of 21890032. Sorry if I do not understand your answer correctly, I updated my code again. Could you point to my code error please. – Banana Feb 22 '17 at 10:49
  • Here is JSON object for your number { "code": "+387", "name": "Bosnia and Herzegovina" } – VVB Feb 22 '17 at 11:03
  • I will suggest you to print JSON for all countries. It will give you idea @ is there anything wrong with your "simCountry"? – VVB Feb 22 '17 at 11:15
  • This line of code also gives the same wrong number: `MyPhoneNumber = tMgr.getLine1Number();` – Banana Feb 22 '17 at 11:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136352/discussion-between-vvb-and-kemo). – VVB Feb 22 '17 at 12:02
0

I checked my settings->status and found out my phone number is unknown, and instead of my phone number it displays IMEI, so I guess I will have the user confirm his phone number by entering it manually.

Banana
  • 2,435
  • 7
  • 34
  • 60