我在哪里? - 获取国家
Android 手机实际上非常清楚它在哪里 - 但有没有一种方法可以通过国家/地区代码之类的内容检索国家/地区?
不需要知道确切的 GPS 位置 - 国家就足够了
我首先想到使用时区,但实际上我需要更多信息,因为如果位置是纽约或利马,情况会有所不同。
问题背景:我有一个使用温度值的应用程序,我想将默认单位设置为摄氏度或华氏度,具体取决于位置是美国还是国外
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?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这将获取为手机设置的国家/地区代码 (手机语言,而不是用户位置):
也可以用 替换 getCountry() getISO3Country() 获取国家/地区的 3 字母 ISO 代码。这将获得 国家/地区名称:
这似乎比其他方法并依赖于手机上的本地化设置,因此如果美国用户在国外,他们可能仍然需要华氏度,这将起作用:)
编者注:此解决方案与位置无关电话的。它是恒定的。当您前往德国旅行时,语言环境不会改变。简而言之:语言环境!=位置。
This will get the country code set for the phone (phones language, NOT user location):
can also replace getCountry() with getISO3Country() to get a 3 letter ISO code for the country. This will get the country name:
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 :)
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.
使用此链接 http://ip-api.com/json ,这将以 json 形式提供所有信息。从这个 json 中你可以轻松获取国家/地区。该网站使用您当前的 IP,它会自动检测 IP 并发回详细信息。
文档 http://ip-api.com/docs/api:json 希望有帮助。
示例 json
note :由于这是第三方解决方案,因此仅在其他解决方案不起作用时才使用。
Use this link http://ip-api.com/json ,this will provide all the information as json. From this json you can get the country easily. This site works using your current IP,it automatically detects the IP and sendback details.
Docs http://ip-api.com/docs/api:json Hope it helps.
Example json
note : As this is a 3rd party solution, only use if others didn't work.
实际上我刚刚发现还有一种获取国家/地区代码的方法,即使用
TelephoneManager
的 getSimCountryIso() 方法:由于它是 sim 代码,因此在前往其他国家/地区时也不应更改。
Actually I just found out that there is even one more way of getting a country code, using the getSimCountryIso() method of
TelephoneManager
:Since it is the sim code it also should not change when traveling to other countries.
首先,获取位置管理器。然后,调用LocationManager.getLastKnownPosition。然后创建一个 GeoCoder 并调用
GeoCoder.getFromLocation
。这是在一个单独的线程中执行的!这将为您提供Address
对象的列表。调用Address.getCountryName
就可以了。请记住,最后一个已知的位置可能有点过时,因此如果用户刚刚越过边界,您可能会暂时不知道。
First, get the LocationManager. Then, call
LocationManager.getLastKnownPosition
. Then create a GeoCoder and callGeoCoder.getFromLocation
. Do this is in a separate thread!! This will give you a list ofAddress
objects. CallAddress.getCountryName
and you got it.Keep in mind that the last known position can be a bit stale, so if the user just crossed the border, you may not know about it for a while.
这是一个基于 LocationManager 的完整解决方案,并作为后备 TelephonyManager 和网络提供商的位置。我使用了 @Marco W. 的上述答案作为后备部分(答案本身就很好!)。
注意:代码包含 PreferencesManager,这是一个从 SharedPrefences 保存和加载数据的帮助器类。我用它来将国家/地区保存到 S"P,只有在国家/地区为空时我才会获取该国家/地区。对于我的产品,我并不真正关心所有边缘情况(用户出国旅行等)。
Here is a complete solution based on the LocationManager and as fallbacks the TelephonyManager and the Network Provider's locations. I used the above answer from @Marco W. for the fallback part(great answer as itself!).
Note: the code contains PreferencesManager, this is a helper class that saves and loads data from SharedPrefrences. I'm using it to save the country to S"P, I'm only getting the country if it is empty. For my product I don't really care for all the edge cases(user travels abroad and so on).
您可以使用
getNetworkCountryIso()
来自 < code>TelephonyManager 来获取手机当前所在的国家/地区(尽管显然这在 CDMA 网络上不可靠)。You could use
getNetworkCountryIso()
fromTelephonyManager
to get the country the phone is currently in (although apparently this is unreliable on CDMA networks).已弃用。使用这个代替:
Is deprecated. Use this instead:
对于某些设备,如果默认语言设置不同(印度人可以设置英语(美国)),则会
给出错误的值。因此此方法不可靠
此外,TelephonyManager 的 getNetworkCountryIso() 方法将不适用于不支持该语言的设备有SIM卡(WIFI平板电脑)。
如果设备没有 SIM 卡,那么我们可以使用时区来获取国家/地区。对于像印度这样的国家,此方法将使用
示例代码来检查该国家是否是印度
(时区 ID:亚洲/加尔各答)
For some devices, if the default language is set different (an indian can set English (US)) then
will give wrong value .So this method is non reliable
Also, getNetworkCountryIso() method of TelephonyManager will not work on devices which don't have SIM card (WIFI tablets).
If a device doesn't have SIM then we can use Time Zone to get the country. For countries like India, this method will work
sample code used to check the country is India or not
(Time zone id : asia/calcutta)
Java(Android 使用的)允许检索当前的 TZ(时区)数据库名称。
尽管您的问题提到时区可能不够具体,但使用此方法,您可以获取用户的国家/地区(在某些情况下甚至是城市),而无需位置权限。
示例 TZ 数据库名称:
Europe/Zurich
显示用户位于瑞士,而Asia/Seoul
显示用户位于韩国。(用户可能不在苏黎世或首尔,可能在其他州/省)
以下是所有可用 TZ 数据库时区的列表:https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
因此,您可以使用以下方式获取 TZ 数据库名称:
您可以将这些名称映射到您选择的国家/地区。
此方法的优点:
context.getResources().getConfiguration().locale.getCountry()
不同,无论用户的区域设置如何,该方法都可以工作。想象一下,如果用户居住在日本并将语言设置为en_US
,您会检测到该用户位于美国而不是日本。参考:Java 8 - tz 数据库时区
请注意,根据此 SO 答案,TZ 数据库时区可能会不时发生变化,因此您应该期待以前未遇到过的新时区。此外,如果用户碰巧去另一个国家/地区旅行,则另一个国家/地区将反映在该方法中。
谢谢!
Java (which is what Android uses) allows for one to retrieve the current TZ (Timezone) database name.
Although your question mentioned that time zones may not be specific enough, using this method, you can get the country (and even city in some cases) of the user without needing location permissions.
A sample TZ Database Name:
Europe/Zurich
reveals that the user is in Switzerland, whileAsia/Seoul
shows that the user is in South Korea.(the user may not be in Zurich or Seoul respectively though, maybe in other states/provinces)
Here is a list of all available TZ Database Time Zones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
So you can get the TZ Database name using:
You can map these to the countries of your choice.
The advantages of this method:
context.getResources().getConfiguration().locale.getCountry()
like others have suggested, this would work regardless of the user's locale. Imagine if the user lived in Japan and set the language toen_US
, you'd detect that the user is in USA instead of Japan.Reference: Java 8 - tz database time zones
Note that according to this SO answer, TZ Database Time Zones may change from time to time, so you should expect new timezones that you have not previously encountered. Also, if the user happens to travel to another country, the other country would be reflected in this method.
Thanks!