How can I retrieve a user's country code on Android? Do I need permissions?
-
https://stackoverflow.com/a/75714418/13082664 – Meet Bhavsar Mar 12 '23 at 16:04
4 Answers
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.
-
5I 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
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).

- 1
- 1
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.

- 1,055
- 12
- 23
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"/>