How to get a country (or its ISO code)?

What is the best way to get the country code? At the moment, I know two ways to get TelephonyManager, and the other is Locale, which is the best and unique way to find the country code in android.

+45
android location country-codes
Aug 31 '12 at 7:25
source share
8 answers

I solved it by IP address. You can get the IP address of your phone. If you use Wi-Fi, then it will be the IP address of the Wi-Fi access point or the IP of your mobile services support server, so from this IP address that you can send to the web service, or something to track the country of this IP address, there are some sources (database) available on the Internet which provides the IP country here, is an example of http://whatismyipaddress.com/ip-lookup .

+5
Sep 07 '12 at 9:40
source share
โ€” -

Try it,

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String countryCode = tm.getSimCountryIso(); 
+68
Sep 06 '12 at 7:12
source share

Huge discussions always talk about this, and I never understand why developers and companies go on a difficult path. The language chosen by the user means the language that he / she wants to always see on his phone. They are not intended for applications in a different language than others or systems.

The choice is very straightforward: use Locale (to select the language chosen by the user as preferred ), or do everything possible to beg them by showing them information in a language in which they already said that they donโ€™t want to see things.

To use the country code:

 Locale.getDefault().getCountry(); 
+25
Sep 09 '12 at 16:37
source share
  TelephonyManager tm = (TelephonyManager)getSystemService(getApplicationContext().TELEPHONY_SERVICE); String countryCode = tm.getNetworkCountryIso(); 

This is better than getSimCountryIso because getSimCountryIso depends on the operator to write the iso country to the SIM card and also supports CDMA networks.

+10
Dec 23 '13 at 7:04 on
source share

I think ip is the best way, because it will give you the country where the phone is at the moment,

If you do it with

  Locale.getDefault().getCountry();, 

you have a country in which the user selects a country so that you can choose England, and you can be in Spain, and maybe you need to know where he is at the moment

Example: imagine that your application can buy something ONLY in England, a user from Spain, but he is on vacation in England and he wants to buy your product ... if you use

  Locale.getDefault().getCountry(); 

that the user will not be able to buy your product, so ip is the best way, I think

+5
May 10 '13 at 12:48
source share

There is a wonderful Reto Meyer article: http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

It describes various methods for obtaining the location of an Android device, including source code. Then, when you have a location, itโ€™s easy to get a country for this - use can use an online web service or an offline database

+4
Sep 04
source share

You can use this code to get ISO2: Locale.getDefault().getCountry()

And to get ISO3: Locale.getDefault().getISO3Country()

Hope this helps

+3
May 27 '16 at 1:29
source share

I know this is a very old question, but I think this will help some developers:

It is very difficult to find a country without GPS and Google Services (China). TelephonyManager will not work if your phone does not have a SIM card.

And Locale will not work if a user in China sets his language as English (the country you receive will be the United States or Great Britain).

If your application requires an Internet connection, there is an option. You can use this API called ip-api . Please go through the documentation there if you plan to use this

There are other APIs like freegeoip api

0
Sep 13 '17 at 6:46
source share



All Articles