Discussion:
[android-security-discuss] etch device country code from android phone
Sirisha Ch
2018-10-04 08:29:18 UTC
Permalink
fetch device country code from android phone is not returning right result
using Locale in Android. I have tried following methods but not working
when there is no sim card in device. Even for Indian device it is showing US
1) 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;
}
2) TelephonyManager tm =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();

3) String locale =
context.getResources().getConfiguration().locale.getCountry(Locale.getDefault());
System.out.println("country = "+locale);
--
You received this message because you are subscribed to the Google Groups "Android Security Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-security-discuss+***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-security-discuss.
For more options, visit https://groups.google.com/d/optout.
'Brian Carlstrom' via Android Security Discussions
2018-10-08 17:31:09 UTC
Permalink
You might want to file something in the issue tracker?
https://source.android.com/setup/contribute/report-bugs

-bri
Post by Sirisha Ch
fetch device country code from android phone is not returning right result
using Locale in Android. I have tried following methods but not working
when there is no sim card in device. Even for Indian device it is showing US
1) 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;
}
2) TelephonyManager tm =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
3) String locale =
context.getResources().getConfiguration().locale.getCountry(Locale.getDefault());
System.out.println("country = "+locale);
--
You received this message because you are subscribed to the Google Groups
"Android Security Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/group/android-security-discuss.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Android Security Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-security-discuss+***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-security-discuss.
For more options, visit https://groups.google.com/d/optout.
Loading...