Where am I? - Get country Where am I? - Get country android android

Where am I? - Get country


/** * 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;}


This will get the country code set for the phone (phones language, NOT user location):

 String locale = context.getResources().getConfiguration().locale.getCountry(); 

can also replace getCountry() with getISO3Country() to get a 3 letter ISO code for the country. This will get the country name:

 String locale = context.getResources().getConfiguration().locale.getDisplayCountry();

This seems easier than the other methods and rely upon the localisation settings on the phone, so if a US user is abroad they probably still want Fahrenheit and this will work :)

Editors note: This solution has nothing to do with the location of the phone. It is constant. When you travel to Germany locale will NOT change. In short: locale != location.


Use this link http://ip-api.com/json ,this will provide all the information as json. From this json you can get the country easily. This site works using your current IP,it automatically detects the IP and sendback details.

Docs http://ip-api.com/docs/api:json Hope it helps.

Example json

{  "status": "success",  "country": "United States",  "countryCode": "US",  "region": "CA",  "regionName": "California",  "city": "San Francisco",  "zip": "94105",  "lat": "37.7898",  "lon": "-122.3942",  "timezone": "America/Los_Angeles",  "isp": "Wikimedia Foundation",  "org": "Wikimedia Foundation",  "as": "AS14907 Wikimedia US network",  "query": "208.80.152.201"}

note : As this is a 3rd party solution, only use if others didn't work.