1

I was wondering if I can get the sim country code in ISO_3166-1 numeric style in android.

I can get the 2 letter country ISO as follows:

TelephonyManager manager = (TelephonyManager)getSystemService(this.TELEPHONY_SERVICE);

String ISO2=manager.getSimCountryIso();
Diego
  • 4,011
  • 10
  • 50
  • 76

3 Answers3

3

Try this code,

TelephonyManager manager = (TelephonyManager)getActivity().getSystemService(getActivity().TELEPHONY_SERVICE);
String ISO2 = manager.getSimCountryIso();
String ISO3 = new Locale("", ISO2).getISO3Country();
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Monu Gupta
  • 153
  • 1
  • 8
0

You can get this using the Locale class as follows:

TelephonyManager manager = (TelephonyManager)getSystemService(this.TELEPHONY_SERVICE);

String ISO2 = manager.getSimCountryIso();

String ISO3 = new Locale("", ISO2).getISO3Country()
Cassie
  • 5,223
  • 3
  • 22
  • 34
0

The proposed solutions using Locale return ISO 3166-1 alpha 3 codes, not ISO 3166-1 numeric ones. The solution I found was using the following library (in our specific case, given it is Apache 2.0 license we just copied the CountryCode class with some slight modifications to make it self-contained):

https://github.com/TakahikoKawasaki/nv-i18n

(found in the following answer).

Then, your code becomes:

TelephonyManager manager = (TelephonyManager)getSystemService(this.TELEPHONY_SERVICE);
String ISO2 = manager.getSimCountryIso();
String ISO3_numeric = CountryCode.getByAlpha2Code(ISO2).getNumeric();
Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50