0

I want to get the country name and country code. For that, I have used the code below:

Country_Language = getApplicationContext().getResources().getConfiguration().locale.getDisplayLanguage();
Country_code= getApplicationContext().getResources().getConfiguration().locale.getCountry(); // get country code

But I always get "US" and "United State" as the country code and country name, while my country is not US.

Any help is appreciated.

Pang
  • 9,564
  • 146
  • 81
  • 122
ADT
  • 255
  • 1
  • 4
  • 14

3 Answers3

1

Try this:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
Pang
  • 9,564
  • 146
  • 81
  • 122
0
//try this
**HardCode Local Configure**
Locale locale = new Locale("en","South Africa");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());


String country_Language = getApplicationContext().getResources().getConfiguration().locale.getDisplayLanguage();
String Country_code= getApplicationContext().getResources().getConfiguration().locale.getCountry();

System.out.println("County Language :"+country_Language);
System.out.println("County Code :"+Country_code);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • 2
    Thanks for reply... But suppose I have selected Language as English(United States)... But if I am not from United States then how..I get my country code... – ADT Nov 11 '13 at 07:14
  • which country are u belong ? – Haresh Chhelana Nov 11 '13 at 07:17
  • Oky I edited my ans see i have change Local throw Hardcode so is forcely allow your app to set Local as South Africa and if you set on device local then just remove Hardcode configure Local code and set Language to English(South Africa) on Your device or emulator – Haresh Chhelana Nov 11 '13 at 07:30
  • 1
    Thanks for efforts but.. it ll create problem when I launch app and user that uses my app is not form South Africa.. – ADT Nov 11 '13 at 07:36
0

But I always get "US" and "United State" as the country code and country name, while my country is not US.

Any help is appreciated.

It happens because you run this code on without SIM device. To solve this problem, you should run this code on a SIM device, after that this code will show current country name and code of device.

Pang
  • 9,564
  • 146
  • 81
  • 122