81

Android 手机实际上确实很清楚它在哪里 - 但有没有办法通过国家代码或国家名称等方式检索国家?

无需知道确切的 GPS 位置 - 国家代码或名称就足够了,我正在使用此代码:

 String locale = context.getResources().getConfiguration().locale.getCountry(Locale.getDefault());
 System.out.println("country = "+locale);

它给了我代码“美国”,但我的设备保存在印度。有没有办法在不使用 GPS 或网络提供商的情况下找到设备当前的国家代码?

因为我用的是平板电脑。

4

10 回答 10

103

你不应该将任何东西传递给getCountry(). 删除Locale.getDefault()

String locale = context.getResources().getConfiguration().locale.getCountry();
于 2012-07-02T12:28:31.997 回答
102

您可以简单地使用此代码,

TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();

如果您当前连接的网络在美国,这将返回“US”。即使没有 SIM 卡也可以使用。

于 2014-10-14T08:31:39.583 回答
58

使用链接http://ip-api.com/json。这将以 JSON 格式提供所有信息。从此 JSON 内容中,您可以轻松获取国家/地区。该站点使用您当前的 IP 地址运行。它会自动检测 IP 地址和发回详细信息。

文档

这就是我得到的:

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

注意 - 由于这是第三方 API,请勿将其用作主要解决方案。而且我不确定它是否免费。

于 2014-11-05T09:30:17.887 回答
14

检查的答案已弃用代码。你需要实现这个:

String locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    locale = context.getResources().getConfiguration().getLocales().get(0).getCountry();
} else {
    locale = context.getResources().getConfiguration().locale.getCountry();
}
于 2017-06-06T14:19:30.927 回答
11

我创建了一个实用程序函数(在我根据语言环境获得不正确国家代码的设备上测试过一次)。

参考CountryCodePicker.java

fun getDetectedCountry(context: Context, defaultCountryIsoCode: String): String {

    detectSIMCountry(context)?.let {
        return it
    }

    detectNetworkCountry(context)?.let {
        return it
    }

    detectLocaleCountry(context)?.let {
        return it
    }

    return defaultCountryIsoCode
}

private fun detectSIMCountry(context: Context): String? {
    try {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        Log.d(TAG, "detectSIMCountry: ${telephonyManager.simCountryIso}")
        return telephonyManager.simCountryIso
    }
    catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

private fun detectNetworkCountry(context: Context): String? {
    try {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        Log.d(TAG, "detectNetworkCountry: ${telephonyManager.networkCountryIso}")
        return telephonyManager.networkCountryIso
    }
    catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

private fun detectLocaleCountry(context: Context): String? {
    try {
        val localeCountryISO = context.getResources().getConfiguration().locale.getCountry()
        Log.d(TAG, "detectLocaleCountry: $localeCountryISO")
        return localeCountryISO
    }
    catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}
于 2018-07-03T07:08:08.933 回答
9

这是一个完整的例子。它尝试从 TelephonyManager(从 SIM 或 CDMA 设备)获取国家代码,如果不可用,则尝试从本地配置中获取它。

private static String getDeviceCountryCode(Context context) {
    String countryCode;

    // Try to get country code from TelephonyManager service
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if(tm != null) {
        // Query first getSimCountryIso()
        countryCode = tm.getSimCountryIso();
        if (countryCode != null && countryCode.length() == 2)
            return countryCode.toLowerCase();

        if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
            // Special case for CDMA Devices
            countryCode = getCDMACountryIso();
        }
        else {
            // For 3G devices (with SIM) query getNetworkCountryIso()
            countryCode = tm.getNetworkCountryIso();
        }

        if (countryCode != null && countryCode.length() == 2)
            return countryCode.toLowerCase();
    }

    // If network country not available (tablets maybe), get country code from Locale class
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        countryCode = context.getResources().getConfiguration().getLocales().get(0).getCountry();
    }
    else {
        countryCode = context.getResources().getConfiguration().locale.getCountry();
    }

    if (countryCode != null && countryCode.length() == 2)
        return  countryCode.toLowerCase();

    // General fallback to "us"
    return "us";
}

@SuppressLint("PrivateApi")
private static String getCDMACountryIso() {
    try {
        // Try to get country code from SystemProperties private class
        Class<?> systemProperties = Class.forName("android.os.SystemProperties");
        Method get = systemProperties.getMethod("get", String.class);

        // Get homeOperator that contain MCC + MNC
        String homeOperator = ((String) get.invoke(systemProperties,
                "ro.cdma.home.operator.numeric"));

        // First three characters (MCC) from homeOperator represents the country code
        int mcc = Integer.parseInt(homeOperator.substring(0, 3));

        // Mapping just countries that actually use CDMA networks
        switch (mcc) {
            case 330: return "PR";
            case 310: return "US";
            case 311: return "US";
            case 312: return "US";
            case 316: return "US";
            case 283: return "AM";
            case 460: return "CN";
            case 455: return "MO";
            case 414: return "MM";
            case 619: return "SL";
            case 450: return "KR";
            case 634: return "SD";
            case 434: return "UZ";
            case 232: return "AT";
            case 204: return "NL";
            case 262: return "DE";
            case 247: return "LV";
            case 255: return "UA";
        }
    }
    catch (ClassNotFoundException ignored) {
    }
    catch (NoSuchMethodException ignored) {
    }
    catch (IllegalAccessException ignored) {
    }
    catch (InvocationTargetException ignored) {
    }
    catch (NullPointerException ignored) {
    }

    return null;
}

另一个想法是尝试像this answer这样的API请求。

参考资料在这里这里

于 2018-09-14T08:09:40.393 回答
6

对于某些设备,如果默认语言设置不同(印度人可以设置英语(美国)),那么

context.getResources().getConfiguration().locale.getDisplayCountry();

会给出错误的值。所以这种方法是不可靠的。

此外,TelephonyManager 的 getNetworkCountryIso() 方法不适用于没有 SIM 卡的设备(Wi-Fi 平板电脑)。

如果设备没有 SIM 卡,那么我们可以使用时区来获取国家/地区。对于像印度这样的国家,这种方法会奏效。

用于检查国家/地区的示例代码是否为印度。

在常量文件中使用以下值

(Constants.INDIA_TIME_ZONE_ID:“亚洲/加尔各答”,Constants.NETWORK_INDIA_CODE:“in”)

在您的活动中,添加以下代码:

private void checkCountry() {

    TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (telMgr == null)
        return;

    int simState = telMgr.getSimState();

    switch (simState) {

        // If a SIM card is not available then the
        // country is found using the timezone ID
        case TelephonyManager.SIM_STATE_ABSENT:
            TimeZone tz = TimeZone.getDefault();
            String timeZoneId = tz.getID();
            if (timeZoneId.equalsIgnoreCase(Constants.INDIA_TIME_ZONE_ID)) {
               // Do something
            }
            else {
               // Do something
            }
            break;

        // If a SIM card is available then the telephony
        // manager network country information is used
        case TelephonyManager.SIM_STATE_READY:

            if (telMgr != null) {
                String countryCodeValue = tm.getNetworkCountryIso();

                // Check if the network country code is "in"
                if (countryCodeValue.equalsIgnoreCase(Constants.NETWORK_INDIA_CODE)) {
                   // Do something
                }
                else {
                   // Do something
                }
            }
            break;
    }
}
于 2018-10-26T11:44:15.467 回答
4

如果您想在不征得任何许可的情况下获得国家代码,您可以选择一种棘手的方式。

该方法简单地使用 API 来获取国家代码,并且没有任何第三方库可以依赖。我们可以为我们创造一个。

在这里,我使用Google Cloud Functions编写了一个 API,而且非常轻松。

第 1 步:创建一个 Google Cloud 帐户,并设置结算(免费套餐就足够了)

第 2 步:创建云函数以获取地理位置

将此基本功能复制到index.js的代码编辑器中:

const cors = require('cors')

function _geolocation(req, res) {
  const data = {
    country_code: req.headers["x-appengine-country"],
    region: req.headers["x-appengine-region"],
    city: req.headers["x-appengine-city"],
    cityLatLong: req.headers["x-appengine-citylatlong"],
    userIP: req.headers["x-appengine-user-ip"]
  }

  res.json(data)
};

exports.geolocation = (req, res) => {
  const corsHandler = cors({ origin: true })

  return corsHandler(req, res, function() {
    return _geolocation(req, res);
  });
};

我们还需要复制package.json定义:

{
  "name": "gfc-geolocation",
  "version": "0.0.1",
  "dependencies": {
    "cors": "^2.8.4"
  }
}

第 3 步:完成,并获取类似于以下内容的 URL:"https://us-central1-geolocation-mods-sdde.cloudfunctions.net/geolocation"

第 4 步:解析 JSON 响应并获取国家代码

响应将如下所示:

{
   "country": "IN",
   "region": "kl",
   "city": "kochi",
   "cityLatLong": "9.9312,76.2673",
   "userIP": "xx.xx.xx.xx"
}

感谢和感谢转至Medium文章:使用 Google Cloud Functions 的基于 IP 的免费地理定位

于 2020-05-29T06:40:00.557 回答
3

无需调用任何 API。您可以从设备所在的设备获取国家代码。只需使用此功能:

 fun getUserCountry(context: Context): String? {
    try {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val simCountry = tm.simCountryIso
        if (simCountry != null && simCountry.length == 2) { // SIM country code is available
            return simCountry.toLowerCase(Locale.US)
        } 
        else if (tm.phoneType != TelephonyManager.PHONE_TYPE_CDMA) { // Device is not 3G (would be unreliable)
            val networkCountry = tm.networkCountryIso
            if (networkCountry != null && networkCountry.length == 2) { // network country code is available
                return networkCountry.toLowerCase(Locale.US)
            }
        }
    }
    catch (e: Exception) {
    }
    return null
}
于 2019-10-23T10:41:22.090 回答
-1

要获取国家代码,我们还可以执行 HTTP 调用(这里我使用第三方链接,您也可以购买服务以获取 JSON 格式的额外信息https://ipinfo.io/)。在这里,当我们调用 API 服务时,它们会跟踪我们的 IP 并相应地提供信息。

<uses-permission android:name="android.permission.INTERNET" />

public String makeServiceCall() {
    String response = null;
    String reqUrl = "https://ipinfo.io/country";
    try {
        URL url = new URL(reqUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());
        response = convertStreamToString(in);
    } catch (MalformedURLException e) {
        Log.e(TAG, "MalformedURLException: " + e.getMessage());
    } catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
    return response;
}
于 2021-02-19T07:23:52.860 回答