i am pretty new to android an now i want that my app show the Address (from coordinates (fused location is used)). at the moment the UI is hanging when i try to get the address and the mobile network quality poor. after a few seconds the address is shown but i dont want that the UI is freezing at enduring address receiving. i know async task could help but i don
t know how to used it the right way with fused location api.
below is my experimental code, this code will be executed via
new GetAddressAsyncTask().execute();
at onResume
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.
}
}