简体   繁体   English

无论用户位置设置如何,如何确定Android设备的当前国家/地区位置?

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

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. 即使用户的设备已关闭GPS并且不允许应用访问其位置,我也需要确定设备所在的国家/地区(iso3)。

I also need to account for tablets that have no sim card and thus cannot use telephonyManager. 我还需要考虑没有SIM卡的平板电脑,因此不能使用telephonyManager。

For this reason I don't believe I can use the location manager (also because of these reasons: LocationManager requestLocationUpdates doesn't work ) 出于这个原因,我不相信我可以使用位置管理器(也是因为这些原因: LocationManager requestLocationUpdates不起作用

The approach I am thinking I will need is to make a simple HTTP request to a third party ip location api: eg http://www.ipinfodb.com/ip_location_api.php or https://freegeoip.net 我认为我需要的方法是向第三方ip位置api发出简单的HTTP请求:例如http://www.ipinfodb.com/ip_location_api.phphttps://freegeoip.net

Is this the best way to do it? 这是最好的方法吗? What is the best open api to use? 什么是最好的开放api使用?

Your approach of third party ip location api seems right to currently. 你的第三方ip location api的方法目前似乎是正确的。 May be This would help you 可能这会对你有所帮助

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

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

You can use the TelephonyManager 您可以使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据用户的当前位置获取国家/地区代码Android - Get country code based on user's current location Android 无法在Android设备上检测到用户当前位置 - Unable to detect user current location on android device 如何检查当前的Android设备是否具有Google的位置服务? - How to check if the current Android device has Google's location service? 如何最准确地确定Android设备的位置? - How to most accurately determine the location of an Android device? 如何在Android中与其他感兴趣的位置一起显示设备的当前位置? - How can I show the device's current location with other interested location in Android? 如何跟踪Facebook当前用户的位置IOS / Android - How to track facebook current user's location IOS/Android Android Maps-如何将地图自动居中到用户的当前位置? - Android Maps - how to automatically center the map to the user's current location? 如何确定Android中的位置? - How to determine location in android? 如何获取用户的国家/地区位置? - How can I get a user's country location? 如何在Android的IntentService中获取用户的当前位置? - how to get current location of user in IntentService in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM