What is the best way to get the current region of the device?
Assume user is in Germany and using Italian as the device's language.
If I use Locale.getDefault()
then country and language are mapped to each other, i.e. the language is it and country is IT. What I want is it and DE/GR(whatever the country code of germany)
Asked
Active
Viewed 3,998 times
3

Ravjit Singh
- 798
- 5
- 17
-
Possible duplicate of [Where am I? - Get country](https://stackoverflow.com/questions/3659809/where-am-i-get-country) – UmAnusorn Jul 11 '17 at 05:15
1 Answers
2
Not all locale countries (combinations) are supported in Android, that is why you do not get the country the way you do it now.
You can get the country code using a different method See for example: Where am I? - Get country
The second answer shows useful code for you: (Credits to @Marco W.)
/**
* Get ISO 3166-1 alpha-2 country code for this device (or null if not available)
* @param context Context reference to get the TelephonyManager instance from
* @return country code or null
*/
public static String getUserCountry(Context context) {
try {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final String simCountry = tm.getSimCountryIso();
if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
return simCountry.toLowerCase(Locale.US);
}
else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
String networkCountry = tm.getNetworkCountryIso();
if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
return networkCountry.toLowerCase(Locale.US);
}
}
}
catch (Exception e) { }
return null;
}

Community
- 1
- 1

Niki van Stein
- 10,564
- 3
- 29
- 62