Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中使用定位服务获取国家名称?_Android_Google Play Services - Fatal编程技术网

如何在Android中使用定位服务获取国家名称?

如何在Android中使用定位服务获取国家名称?,android,google-play-services,Android,Google Play Services,在我的应用程序中,我正在使用支付网关。根据用户国家/地区我必须更改付款方式类型。通过使用Locale我可以获得国家/地区,但它给出的设备配置/制造的国家/地区对我来说并不有用。因此我尝试谷歌播放服务(位置服务)。如何使用此API仅获取国家/地区名称。我不想要经纬度等。谢谢你可以用它 PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi .getCurrentPlace(mGoogleApi

在我的
应用程序中
,我正在使用
支付网关
。根据
用户国家/地区
我必须更改
付款方式类型
。通过使用
Locale
我可以获得国家/地区,但它给出的
设备配置/制造的国家/地区对我来说并不有用。因此我尝试
谷歌播放服务(位置服务)
。如何使用此
API
仅获取
国家/地区名称
。我不想要经纬度等。谢谢你可以用它

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
    .getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
  @Override
  public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
     for (PlaceLikelihood placeLikelihood : likelyPlaces) {
         Log.i(TAG, String.format("Place '%s' has likelihood: %g",
         placeLikelihood.getPlace().getName(),
         placeLikelihood.getLikelihood()));
     }

     likelyPlaces.release();
   }
});
pendingreult结果=Places.PlaceDetectionApi
.getCurrentPlace(mgoogleapClient,空);
result.setResultCallback(新的ResultCallback(){
@凌驾
public void onResult(PlaceLikelihoodBuffer likelyPlaces){
for(place可能性place可能性:likelyPlaces){
Log.i(TAG,String.format(“位置“%s”的可能性为:%g”),
place.getPlace().getName(),
placelikelibility.getlikelibility());
}
likelyPlaces.release();
}
});

如果设备是电话,那么您可以通过这种方式获取国家/地区信息

 public static String getUserCountry(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        final String simCountry = tm.getSimCountryIso();
        if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
            return simCountry.toLowerCase(Locale.US);
        }
        else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
            if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                return networkCountry.toLowerCase(Locale.US);
            }
        }
    }
    catch (Exception e) { }
    return null;
}
然后,您可以访问它:

Locale loc = new Locale("", getUserCountry(this));

TextView.setText(loc.getDisplayCountry());
PS: 您需要添加权限:


如果您只需要用户的国家/地区,为什么不获取当前SIM卡的国家/地区?这是一种可能的选择。如果用户使用平板电脑,我该如何获取?谢谢您的代码。让我试试。。我需要为此添加任何权限吗。很高兴它确实有帮助!:)谢谢你的建议。让我试试你的代码。有添加权限吗?