Java (which is what Android uses) allows for one to retrieve the current TZ (Timezone) database name.
Although your question mentioned that time zones may not be specific enough, using this method, you can get the country (and even city in some cases) of the user without needing location permissions.
A sample TZ Database Name:
Europe/Zurich
reveals that the user is in Switzerland, while Asia/Seoul
shows that the user is in South Korea.
(the user may not be in Zurich or Seoul respectively though, maybe in other states/provinces)
Here is a list of all available TZ Database Time Zones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
So you can get the TZ Database name using:
public String usersCountryByTzDbName() {
return ZoneId.systemDefault().getId();
}
You can map these to the countries of your choice.
The advantages of this method:
- Unlike
context.getResources().getConfiguration().locale.getCountry()
like others have suggested, this would work regardless of the user's locale. Imagine if the user lived in Japan and set the language to en_US
, you'd detect that the user is in USA instead of Japan.
- Works on Wi-Fi only devices (which would not work if you used the telephony manager API)
Reference: Java 8 - tz database time zones
Note that according to this SO answer, TZ Database Time Zones may change from time to time, so you should expect new timezones that you have not previously encountered. Also, if the user happens to travel to another country, the other country would be reflected in this method.
Thanks!