如何正确创建AsyncTask

时间:2016-11-11 00:58:22

标签: java android android-asynctask osmdroid

我是Java的新手,因为这个问题属于我工作的时间敏感项目,所以我没有时间学习AsyncTasks的所有内容。 所以我的问题是:

如何使用以下代码构建AsyncTask

目标是在地图上绘制路线。我用ArrayList填充了两个Geopoints(路线的起始位置和目的地)。 roadManager应该将这些航点发送给服务器,该服务器将回送路线。

buildRoadOverlay是最终在地图上绘制路线的方法。

    RoadManager roadManager = new OSRMRoadManager(this);

    ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>();
    GeoPoint myLocation = new GeoPoint(51.488978, 6.746994);
    waypoints.add(Location);
    waypoints.add(myLocation);
    Road road = roadManager.getRoad(waypoints);

我想这必须进入onPostExecute方法,对吧?:

    Polyline roadOverlay = RoadManager.buildRoadOverlay(road);
    map.getOverlays().add(roadOverlay);

来自上层代码的变量location来自不同的方法,我打算从中启动异步任务。这意味着,我需要在调用它时将变量传递给AsyncTask,我也不确定该怎么做。

这是变量location

的初始化
GeoPoint Location = new GeoPoint(Double.parseDouble(place.getLongitude()), 
    Double.parseDouble(place.getLatitude()));

2 个答案:

答案 0 :(得分:2)

将耗时的任务放在onPostExecute()的doInBackground(),udpate视图中。

public void drawRouteAsync() {
    GeoPoint location = new GeoPoint(Double.parseDouble(place.getLongitude()),
            Double.parseDouble(place.getLatitude()));
    GeoPoint myLocation = new GeoPoint(51.488978, 6.746994);

    new RouteAsyncTask().execute(location, myLocation);
}

private class RouteAsyncTask extends AsyncTask<GeoPoint, Void, Road> {
    @Override
    protected Road doInBackground(GeoPoint... params) {
        ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>();
        waypoints.add(params[0]); // location
        waypoints.add(params[1]); // myLocation
        RoadManager roadManager = new OSRMRoadManager(mContext); // your context
        Road road = roadManager.getRoad(waypoints); // time consuming
        return road;
    }

    @Override
    protected void onPostExecute(Road road) {
        Polyline roadOverlay = RoadManager.buildRoadOverlay(road);
        map.getOverlays().add(roadOverlay); // update view
    }
}

答案 1 :(得分:1)

AsyncTask 有3个重要方法:

  1. protected void onPreExecute() //主线程
  2. protected E doInBackground(T... params) // async thread
  3. protected void onPostExecute(E result) //主线程
  4. ET将在实施过程中定义。

    知道这一点,你的AsincTask应该看起来像这样:

    public class GetRouteTask extends AsyncTask<GeoPoint, Void, Integer> {
    
        private Context mContext;
        private OnGetRouteCompleted delegate;
        private Road route;
    
        public GetRouteTask (Context context, OnGetRouteCompleted delegate) {
            this.delegate = delegate;
            this.mContext = context;
        }
    
        @Override
        protected Integer doInBackground(GeoPoint... params) {
            ArrayList<GeoPoint> wayPoints = new ArrayList<GeoPoint>();
            wayPoints.add(params[0]); // pointFrom 
            wayPoints.add(params[1]); // pointTo 
    
            try {
                RoadManager roadManager = new OSRMRoadManager(mContext);
                route = roadManager.getRoad(waypoints);
                return 1;
            } catch (JSONException e) {
                return -1;
            }
        }
    
        @Override
        protected void onPostExecute(Integer success) {
            if (success == 1) {
                delegate.onGetRouteCompleted(true, route);
            } else {
                delegate.onGetRouteCompleted(false, null);
            }
            Polyline roadOverlay = RoadManager.buildRoadOverlay(road);
            map.getOverlays().add(roadOverlay); // update view
        }
    
        public interface OnGetRouteCompleted {
            public void onGetRouteCompleted(boolean success, Route route);
        }
    }
    

    这就是你如何使用它:

    public class SomeActivity extend Activity implements OnGetRouteCompleted {
    
        // Methods and properties...
    
        public void drawRouteAsync() {
            GeoPoint pointFrom = new GeoPoint(51.489878, 6.143294);
            GeoPoint pointTo = new GeoPoint(51.488978, 6.746994);
    
            new GetRouteTask(getApplicationContext(), this).execute(new GeoPoint(){pointFrom , pointTo});
        }
    
        @Override
        public void onGetRouteCompleted(boolean success, Route route) {
            if (success) {
                Polyline roadOverlay = RoadManager.buildRoadOverlay(route);
                map.getOverlays().add(roadOverlay); // update view
            }
        }
    }