6

当我的应用程序运行时,我想检测当前位置的国家名称并为该特定国家名称做一些事情,我可以使用地理编码器、gps 轻松完成。但我想从 Locale 或如果有更多可用的东西得到它。

4

3 回答 3

15

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

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

这就是我得到的。

{
"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": ""

}

于 2014-11-05T09:26:42.057 回答
5

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

于 2012-05-25T14:07:56.753 回答
1

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

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

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

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

于 2014-12-20T23:23:45.967 回答