Get country TelephonyManager vs locale.getCountry(

2019-09-13 13:24发布

问题:

what is the difference between:

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

and

TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();

What I need is like this (hard to test it). The user is from US and travels to France with his sim card and uses his sim card abroad, the device is in English (US) all the time. I need when he arrives to France get France from code. How to do it right?

回答1:

Use TelephonyManager.getNetworkCountryIso()

Returns the ISO country code equivalent of the current registered operator's MCC (Mobile Country Code).

Availability: Only when user is registered to a network. Result may be unreliable on CDMA networks (use getPhoneType() to determine if on a CDMA network).

Or LocationManager.getLastKnownLocation(), for example.

String locale = context.getResources().getConfiguration().locale.getCountry(); relies on the Locale of the device, which is going to be the same around the globe, unless user changes it manually.