How to determine an Android device's current country location regardless of user location settings?

一世执手 提交于 2019-12-22 05:07:42

问题


I need to determine the country (iso3) the device is in even if the user's device has GPS turned off and does not allow apps to access it's location.

I also need to account for tablets that have no sim card and thus cannot use telephonyManager.

For this reason I don't believe I can use the location manager (also because of these reasons: LocationManager requestLocationUpdates doesn't work)

The approach I am thinking I will need is to make a simple HTTP request to a third party ip location api: e.g. http://www.ipinfodb.com/ip_location_api.php or https://freegeoip.net

Is this the best way to do it? What is the best open api to use?


回答1:


Your approach of third party ip location api seems right to currently. May be This would help you

http://ip-api.com/docs/api:json

http://ip-api.com/json




回答2:


You can use the TelephonyManager

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

Update: Since you cannot use telephony I would suggest trying this, as it get's the users input from when they setup the device.

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


来源:https://stackoverflow.com/questions/28906980/how-to-determine-an-android-devices-current-country-location-regardless-of-user

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