我在哪里? - 获取国家 [英] Where am I? - Get country

查看:146
本文介绍了我在哪里? - 获取国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是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()与<一个href="http://developer.android.com/reference/java/util/Locale.html#getISO3Country%28%29">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 :)

这篇关于我在哪里? - 获取国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆