5

How can I retrieve a user's country code on Android? Do I need permissions?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Caroline
  • 3,763
  • 5
  • 19
  • 25

4 Answers4

11

To get the country code stored in the sim card of the phone you can try

TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getSimCountryIso();

You can try also:

 Locale locale = Locale.getDefault();
 locale.getCountry();

This returns the data from the language/Country stated from the user and not the physical location.

vrunoa
  • 766
  • 8
  • 15
weakwire
  • 9,284
  • 8
  • 53
  • 78
  • 5
    I don´t get this kind of answers. What about wifi devices? What about devices without sim? what about devices with a sim but in a foreign country? – Sotti May 11 '14 at 19:43
1

Latitude and Longitude of the current location

Will get you the latitude and longitude.

I'd suggest using Google Map's reverse geocoding for getting the country, and then using a lookup table if you want to get some country code (I'd assume the country's telephone code, but I could be wrong).

Community
  • 1
  • 1
0

Best practice from the: Localization documentation recommends using the Locale set here:

context.getResources().getConfiguration().locale;

Furthermore, this field is also mentioned here. You can then call getCountry() on it to obtain the two-letter uppercase ISO country code as defined by ISO 3166-1. Note that java doc says it may return an empty String if the Locale doesn't correspond to specific country.

fr1550n
  • 1,055
  • 12
  • 23
0

You can try the following function. It's returning your country ISO code:

  TelephonyManager telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

        public String getSimCountryIso() {

    return telephonyManager.getSimCountryIso();

}

Add permission below into your AndroidManifest.xml file.

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
clemens
  • 16,716
  • 11
  • 50
  • 65
Quantum4U
  • 35
  • 4