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.
这篇关于我在哪里?- 获取国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!