Reliable method to get the country the user is in?

落花浮王杯 提交于 2019-12-28 05:22:45

问题


I usually get the country from the device's language. It works but now I have to recognize Brazil. And most of the devices only have portuguese (pt_PT), and no portuguese (Brazil) option.

I checked this thread: Where am I? - Get country

The methods

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

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

Are still language-only, doesn't help.

There's also the suggestion with the sim-card, but I'm not sure if this will work reliably (do all sim cards have this unique identification?), it's also a bit not exactly what I need because the user can't change it (which is the case if it was a setting), and it will exclude users using a device without sim-card (maybe they just use WLAN).

There's also geolocation suggestion, but this will probably not work in devices which have deactivated it. Or am I wrong?

If nothing else helps I would make a dialog or menu setting in my app so the user can select it there. But I would first like to confirm if there's any reliable possibility with the device.


回答1:


You can pull the location from the phone network:

TelephonyManager.getNetworkCountryIso()

Or from the SIM card:

TelephonyManager.getSimCountryIso()

Or, if you have the user's phone number, you may be able to match it to the country through this data.

Ideally, you could use all three of these (in some order, perhaps SIM, Phone #, then Network), and if none works, use reverse geolocation as a last resort.




回答2:


Try this:

 TelephonyManager teleMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 if (teleMgr != null){
     countryISOCode = teleMgr.getSimCountryIso();
 }

Now, countryISOCode would contain one of the following similar to the ISO 3166 country codes as per this Wikipedia entry.




回答3:


Geolocation would be the most effective and unobtrusive. You could always use geolocation, and if the user has it disabled display your dialog asking for their country. The less you bug the user for information the better.




回答4:


TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.getNetworkCountryIso();

As answered by Eric, the above code is the best approach. It is also worth noting that,

mTelephonyManager.getSimCountryIso()

should not be used as this would indicate the home country of the SIM provider (E.g. A Vodaphone UK SIM would return "gb", A Vodaphone Germany SIM would return "de") and not the current location (Country) of the device. This difference is significant when the user is roaming.




回答5:


It's working on me.

String countryISOCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TelephonyManager teleMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    if (teleMgr != null){
        countryISOCode = teleMgr.getSimCountryIso();
    }

............................



来源:https://stackoverflow.com/questions/11872483/reliable-method-to-get-the-country-the-user-is-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!