0
指で地図サーフィンを扱うが、いつかUIスレッドは非同期タスクコードGoogleマップOnCameraChangeListenerジオコーダAsynTaskブロックUIスレッドは
googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
ShowSearchLocationLoader(true);
LatLng latlngCamera = new LatLng(cameraPosition.target.latitude, cameraPosition.target.longitude);
if(Utilities.isConnected == true) {
cameraChangedTask = new CameraChangedTask();
cameraChangedTask.execute(latlngCamera);
} else {
Toast.makeText(ActivityHome.this, R.string.connection_error, Toast.LENGTH_SHORT).show();
}
}
});
}
private class CameraChangedTask extends AsyncTask<LatLng, Void, Void> {
List<Address> address;
@Override
protected Void doInBackground(LatLng... latLngs) {
Geocoder coder = new Geocoder(currentContext);
LatLng location = latLngs[0];
try {
address = coder.getFromLocation(location.latitude, location.longitude, 1);
} catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
if (address != null) {
if (address.isEmpty()) {
mAutocompleteView.setHint("Waiting for Location");
} else {
if (address.size() > 0 && address.get(0) != null) {
mAutocompleteView.setHint(address.get(0).getAddressLine(0) + ", " + address.get(0).getAddressLine(1));
setPickupaddress(address.get(0).getAddressLine(0) + ", " + address.get(0).getAddressLine(1));
} else {
mAutocompleteView.setHint(address.get(0).getFeatureName());
setPickupaddress(address.get(0).getFeatureName());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
ShowSearchLocationLoader(false);
}
}
を実行するために入る前に約30秒間ブロックされているコードは、
私の問題の解決方法はこちらです:cameraChangedTask = new CameraChangedTask(); cameraChangedTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR、latlngCamera); http://stackoverflow.com/questions/12168693/asynctasks-blocking-others –