Reputation: 255
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.
Upvotes: 0
Views: 13223
Reputation: 1347
Try this:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 24848
//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);
Upvotes: 0