Java 请求安卓中最近的城市
我需要在android应用程序中仅使用网络提供商计算附近的城市。 我用下面的代码实现了这一点,但它会使应用程序运行得非常慢,而且只在某些时候起作用Java 请求安卓中最近的城市,java,android,locationmanager,Java,Android,Locationmanager,我需要在android应用程序中仅使用网络提供商计算附近的城市。 我用下面的代码实现了这一点,但它会使应用程序运行得非常慢,而且只在某些时候起作用 public Locator(final Context c) { LocationManager locationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE); Location location = locat
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();
}
}
有什么想法吗
提前谢谢。你可以试试。这是一个