Where am I? - Get country(我在哪里?- 获取国家)
问题描述
Android 手机实际上确实很清楚它在哪里 - 但是有没有办法通过国家代码之类的东西来检索国家?
An android mobile actually does know quite well where it is - but is there a way of retrieving the country by something like a country code?
无需知道准确的 GPS 位置 - 国家 就足够了
No need of knowing the exact GPS position - the country is sufficient
我最初考虑使用时区,但实际上我需要更多信息,因为如果位置是纽约或利马,它会有所不同.
I first thought of using the time zone, but actually I need more information than that since it makes a difference if the location is New York or Lima.
问题的背景: 我有一个使用温度值的应用程序,我想将默认单位设置为摄氏度或华氏度,具体取决于位置是美国还是国外
The background of the question: I have an application that uses temperature values, and I'd like to set the default unit either to Celsius or Fahrenheit, depending on whether the location is US or outside
推荐答案
这将得到 国家代码 为手机设置(手机语言,而不是用户位置):
This will get the country code set for the phone (phones language, NOT user location):
String locale = context.getResources().getConfiguration().locale.getCountry();
也可以用 getISO3Country() 获取国家/地区的 3 个字母 ISO 代码.这将获得 国家/地区名称:
can also replace getCountry() with getISO3Country() to get a 3 letter ISO code for the country. This will get the country name:
String locale = context.getResources().getConfiguration().locale.getDisplayCountry();
这似乎比其他方法更容易,并且依赖于手机上的本地化设置,所以如果美国用户在国外,他们可能仍然想要华氏度,这会起作用:)
This seems easier than the other methods and rely upon the localisation settings on the phone, so if a US user is abroad they probably still want Fahrenheit and this will work :)
编者注:此解决方案与手机的位置无关.它是恒定的.当您前往德国时,语言环境不会改变.简而言之:locale != location.
Editors note: This solution has nothing to do with the location of the phone. It is constant. When you travel to Germany locale will NOT change. In short: locale != location.
这篇关于我在哪里?- 获取国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我在哪里?- 获取国家
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
