Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliable method to get the country the user is in?

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.

like image 373
User Avatar asked Aug 08 '12 20:08

User


1 Answers

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.

like image 77
Cat Avatar answered Sep 23 '22 00:09

Cat