我需要仅使用网络提供商在 android 应用程序中计算附近的城市。我设法用下面的代码来做到这一点,但它使应用程序变得非常慢,而且它只在某些时候工作。
public Locator(final Context c) {
LocationManager locationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Geocoder gcd = new Geocoder(c, Locale.getDefault());
List addresses;
try {
addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0) {
if(addresses.get(0).getLocality()!=null){
Locator.setCity(addresses.get(0).getLocality());
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
有任何想法吗?
提前致谢。