我在移动到 AsyncTask 的方法上遇到问题。我已经完成了 doInBackground,但我需要将该方法的其余部分移至 onPostExecute ...我只是不知道该怎么做。
这是我原来的方法。
private void ShowFuelStopAndRoute(String location) throws IOException {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
addresses = geocoder.getFromLocationName(location, 1);
if(addresses.size() > 0) {
BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.fillingstation);
currentFuelLat= addresses.get(0).getLatitude();
currentFuellong= addresses.get(0).getLongitude();
LatLng toPostion = new LatLng(currentFuelLat, currentFuellong);
GMapV2Direction md = new GMapV2Direction();
AsyncDoc asyncDoc = new AsyncDoc(MapViewActivity.this, currentLocation, toPostion, GMapV2Direction.MODE_DRIVING);
ArrayList<LatLng> directionPoint = md.getDirection(doc);
PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.BLUE);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}
map.addPolyline(rectLine);
Marker fuelMarker = map.addMarker(new MarkerOptions().position(toPostion).icon(subwayBitmapDescriptor).title("Fueling Station, " + location));
}
}
我将以下行移动到不同的类和异步类中
Document doc = md.getDocument(currentLocation, toPostion, GMapV2Direction.MODE_DRIVING);
这已被替换为:
return DALController.sharedInstance().getDocument(startLoc, endLoc, mode);
我有这么多 AsyncTask Complete 如下所示:
private class AsyncDoc extends AsyncTask<Void, Void, Document > {
public String mode;
public LatLng startLoc;
public LatLng endLoc;
public AsyncDoc (Activity activity, LatLng startLoc, LatLng endLoc, String mode){
this.startLoc = startLoc;
this.endLoc = endLoc;
this.mode = mode;
dialog = new ProgressDialog(activity);
}
private Activity activity;
private ProgressDialog dialog;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
if (! (activity).isFinishing()) {
this.dialog.show();
}
}
@Override
protected Document doInBackground(Void... params) {
return DALController.sharedInstance().getDocument(startLoc, endLoc, mode);
}
@Override
protected void onPostExecute(Document result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
但我需要确保在 doInBackground 完成之前其余代码不会运行:
ArrayList<LatLng> directionPoint = md.getDirection(doc);
PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.BLUE);
for(int i = 0 ; i < directionPoint.size() ; i++) {
rectLine.add(directionPoint.get(i));
}
map.addPolyline(rectLine);
Marker fuelMarker = map.addMarker(new MarkerOptions().position(toPostion).icon(subwayBitmapDescriptor).title("Fueling Station, " + location));
如何确保 doInBackground 在我的 showfuelstopandroute 方法的其余部分完成之前完成?