在我的Application
中,我使用Payment Gateway
。基于User Country
,我必须更改Payment method type
。使用Locale
我可以获得国家/地区,但它会为Device Configured/Manufactured
国家/地区提供对我无用的国家/地区。所以我正在尝试Google Play Services (Location Services)
。如何使用此Country name
仅获得API
。我不想要Latitude Longitude等。谢谢
答案 0 :(得分:3)
如果该设备是手机,那么您可以通过这种方式获得该国家/地区。
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 强>: 你需要添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
答案 1 :(得分:0)
您可以使用Google Current Place 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();
}
});