所以我正在申请获得当前职位。下面的代码工作正常
字符串字符串地址 = "";
公共无效getLocation(查看视图){
最终 LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
标准 kriteria = new Criteria();
kriteria.setAccuracy(Criteria.ACCURACY_FINE);
kriteria.setAltitudeRequired(false);
kriteria.setBearingRequired(false);
kriteria.setCostAllowed(true);
kriteria.setPowerRequirement(Criteria.POWER_LOW);
最终字符串提供者 = lm.getBestProvider(kriteria, true);
最终位置 lokasi = lm.getLastKnownLocation(provider);
updateWithNewLocation(lokasi);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, ll);
edittext_position.setText(stringAddress);
}
私人无效updateWithNewLocation(位置
如果(lokasi!= null){
双纬度 = lokasi.getLatitude();
双 lng = lokasi.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
尝试{
列出地址 = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
如果(地址。大小()> 0){
地址地址=addresses.get(0);
sb.append(address.getAddressLine(0));
stringAddress= sb.toString();
}
} 捕捉(异常 e){
}
}
}
私人最终 LocationListener ll = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
}
公共无效onProviderEnabled(字符串提供者){
}
公共无效onProviderDisabled(字符串提供者){
updateWithNewLocation(null);
}
公共无效onLocationChanged(位置位置){
updateWithNewLocation(位置);
}
}
但问题是每次我调用 getLocation() 函数时,应用程序都会在返回结果之前挂起几秒钟。我知道使用 aSyncTask 解决这个问题,但我不知道如何开始。感谢你的帮助。
谢谢