java 如何在不使用 GPS 的情况下以编程方式检测 android 设备当前位于哪个国家?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10755534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:24:31  来源:igfitidea点击:

How to programmatically detect in which country the android device is located currently without using GPS?

javaandroid

提问by Reyjohn

When my app runs, I want to detect the current location's country name and do some stuff for that specific country name, I can easily do it using geocoder, gps. But I want to get it from Locale or if anything more is available.

当我的应用程序运行时,我想检测当前位置的国家/地区名称并针对该特定国家/地区名称执行一些操作,我可以使用地理编码器、gps 轻松完成。但我想从 Locale 获取它,或者如果有更多可用的东西。

采纳答案by spot

I dont know if Android API allowes you to check the Provider Code of the associated GSM Network. If this is possible you could use this Code an check i.e. against this List : http://www.techgsm.com/page/gsm-provider-codes/network-provider-identify-codes.htmlto find out in which country the Cellphone is.

我不知道 Android API 是否允许您检查关联的 GSM 网络的提供商代码。如果可能,您可以使用此代码检查此列表:http: //www.techgsm.com/page/gsm-provider-codes/network-provider-identify-codes.html以找出哪个国家/地区手机是。

回答by shine_joseph

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.

使用此链接http://ip-api.com/json,这将提供 json 形式的所有信息。从这个 json 你可以很容易地得到这个国家。该站点使用您当前的 IP 工作,它会自动检测 IP 和发回详细信息。

Docs http://ip-api.com/docs/api:jsonHope it helps.

文档http://ip-api.com/docs/api:json希望它有所帮助。

This is what I got.

这就是我得到的。

{
"as": "AS55410 C48 Okhla Industrial Estate, New Delhi-110020",
"city": "Kochi",
"country": "India",
"countryCode": "IN",
"isp": "Vodafone India",
"lat": 9.9667,
"lon": 76.2333,
"org": "Vodafone India",
"query": "123.63.81.162",
"region": "KL",
"regionName": "Kerala",
"status": "success",
"timezone": "Asia/Kolkata",
"zip": ""

}

}

回答by Technologeeks

You can (and should) use Android's country detector service. The service consists of three methods, all defined in their .aidl, which enable you to get the country code, register a listener, or remove it. Specifically, you want the first - Country detectCountry();which returns an android.location.Country object (frameworks/base/location/java/android/location/Country.java in the AOSP). I'm assuming you know how to call methods programmatically (you'll need getSystemService ("country_detector")).

您可以(并且应该)使用 Android 的国家检测器服务。该服务由三个方法组成,均在其 .aidl 中定义,使您能够获取国家/地区代码、注册侦听器或删除它。具体来说,您需要第一个 -Country detectCountry();它返回一个 android.location.Country 对象(AOSP 中的frameworks/base/location/java/android/location/Country.java)。我假设您知道如何以编程方式调用方法(您将需要 getSystemService ("country_detector"))。

To test from the command line, try:

要从命令行进行测试,请尝试:

shell@flounder:/ $ service call country_detector 1
Result: Parcel(
  0x00000000: 00000000 00000001 00000002 00530055 '............U.S.'
  0x00000010: 00000000 00000003 1d809dda 00000000 '................')

The object serialization is basically nothing more than the 2 letter country code (here: US), the source (here, 3, implying this was gleaned from the Locale), and a timestamp (1d809dda...) which really isn't that important. You don't have to worry about serialization since the object is Parcelable.

对象序列化基本上无非是 2 个字母的国家/地区代码(此处:US)、来源(此处为 3,暗示这是从 Locale 中收集的)和时间戳(1d809dda...),实际上并非如此重要的。您不必担心序列化,因为对象是 Parcelable。