1

To pre-fill a phone-number field in our application we'd like to use the SIM-stored phone number (when available). For this we used the common solution also outlined in this popular question/answer:

TelephonyManager tMgr = (TelephonyManager) mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

The issue with this, however, is that this string will also contain the international dialing prefix, i.e. will look something like this:

'+[prefix][phoneNumber]'

As the prefix varies between 1 and 3 digits, it's we can't simply trim it...

--> Is there a way to get to the phone number from the SIM without the country dialing prefix?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
fgysin
  • 11,329
  • 13
  • 61
  • 94

1 Answers1

1

You can get the number with libphonenumber-android, something like this:

PhoneNumberUtil util = PhoneNumberUtil.createInstance(getApplicationContext());

try {
  // Indonesia phone number
  final Phonenumber.PhoneNumber phoneNumber = util.parse("+62821234567", "ID");

  // now you get the number without country code
  long nationalNumber = phoneNumber.getNationalNumber();
} catch (NumberParseException e) {
   e.printStackTrace();
}

I haven't tested the code though.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96