Android 将fused location geocoder运行到异步任务中
我对android非常陌生,现在我希望我的应用程序显示地址(从坐标(使用融合位置))。目前,当我试图获取地址和移动网络质量差时,UI挂起。几秒钟后,地址会显示出来,但我不希望UI在持久的地址接收时冻结。我知道异步任务可能会有所帮助,但我不知道如何在融合位置api中正确使用它 下面是我的实验代码,该代码将通过Android 将fused location geocoder运行到异步任务中,android,android-asynctask,geolocation,location,android-fusedlocation,Android,Android Asynctask,Geolocation,Location,Android Fusedlocation,我对android非常陌生,现在我希望我的应用程序显示地址(从坐标(使用融合位置))。目前,当我试图获取地址和移动网络质量差时,UI挂起。几秒钟后,地址会显示出来,但我不希望UI在持久的地址接收时冻结。我知道异步任务可能会有所帮助,但我不知道如何在融合位置api中正确使用它 下面是我的实验代码,该代码将通过 新建GetAddressAsyncTask().execute()在恢复时 class GetAddressAsyncTask extends AsyncTask<Void, voi
新建GetAddressAsyncTask().execute()代码>在恢复时
class GetAddressAsyncTask extends AsyncTask<Void, void, void> {
double lat = mLastLocation.getLatitude();
double lng = mLastLocation.getLongitude();
@Override
protected Void doInBackground(Void... params) {
// Do some background work
Geocoder geoCoder = new Geocoder(MainActivity.this, Locale.getDefault());
StringBuilder builder = new StringBuilder();
try {
List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
int maxLines = address.get(0).getMaxAddressLineIndex();
for (int i=0; i<maxLines; i++) {
String addressStr = address.get(0).getAddressLine(i);
builder.append(addressStr);
builder.append(" ");
}
} catch (IOException e) {
// Handle IOException
} catch (NullPointerException e) {
// Handle NullPointerException
}
return null;
}
@Override
protected void onPostExecute(String result) {
String finalAddress = builder.toString(); //This is the complete address.
addressField.setText(finalAddress); //This will display the final address.
}
}
类GetAddressAsyncTask扩展了AsyncTask{
双lat=mLastLocation.getLatitude();
double lng=mLastLocation.getLongitude();
@凌驾
受保护的Void doInBackground(Void…参数){
//做一些背景工作
Geocoder Geocoder=新的Geocoder(MainActivity.this,Locale.getDefault());
StringBuilder=新的StringBuilder();
试一试{
列表地址=地理编码器。getFromLocation(lat,lng,1);
int maxLines=address.get(0.getMaxAddressLineIndex();
对于(int i=0;i