我在哪里? - 获取国家我在、国家

2023-09-11 12:25:21 作者:黑白昼夜循环ゝ

这是Android手机实际上并一清二楚,它在哪里? - 但有检索国家通过类似的国家code的一种方式

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

推荐答案

这将获得的国code :

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

也可以替换getCountry()与getISO3Country()获得3个字母的ISO code为国家。这将让该国名:

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 :)