“我需要根据用户的位置切换基URL,并更改其他UI组件”。
我尝试了整个Build Config
,但我没有找到任何似乎有效的东西。
发布于 2019-07-09 22:24:51
首先,您必须将服务器添加到strings.xml文件中,然后您可以使用此文件来获取位置、国家/地区名称,之后在切换的情况下,您可以更改服务器
@Override
protected void onResume() {
String country_name = null;
LocationManager lm =
(LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Geocoder geocoder = new Geocoder(getApplicationContext());
for(String provider: lm.getAllProviders()) {
@SuppressWarnings("ResourceType") Location location = lm.getLastKnownLocation(provider);
if(location!=null) {
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if(addresses != null && addresses.size() > 0) {
country_name = addresses.get(0).getCountryName();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Toast.makeText(getApplicationContext(), country_name, Toast.LENGTH_LONG).show();
}
https://stackoverflow.com/questions/56954559
复制相似问题