我遇到了一个方法,我正在转向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));
在showfuelstopandroute方法的其余部分完成之前,如何确保doInBackground完成?
答案 0 :(得分:0)
在AsyncTask中,onPostExecute()方法仅在doInBackground()完成后运行。你面临的问题是什么?
答案 1 :(得分:0)
如何确保doInBackground在我的其余部分之前完成 showfuelstopandroute方法完成了吗?
您应该在doInBackground执行完成后移动要执行的onPostExecute
内的所有代码。为:
@Override
protected void onPostExecute(Document result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
// do your work here after doInBackground execution
ArrayList<LatLng> directionPoint = md.getDirection(result);
....
}
答案 2 :(得分:0)
正如其他人所提到的,你可以在postExecute-method中移动应该在任务之后执行的整个代码。 另一种方法是让你的活动有一个帽子方法,任务可以在其onPostExecute()中调用