1

我必须在 AsyncTask 中使用方向()函数来避免 UI 线程上的网络处理。我无法做这件事。我的代码是

    public class MainActivity extends MapActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MapView mapView = (MapView) findViewById(R.id.mapview); //or you can declare it directly with the API key
        Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));
        RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
        mapView.getOverlays().add(routeOverlay);
        mapView.invalidate();
    }

    private Route directions(final GeoPoint start, final GeoPoint dest) {
        Parser parser;
        //https://developers.google.com/maps/documentation/directions/#JSON <- get api
        String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
        final StringBuffer sBuf = new StringBuffer(jsonURL);
        sBuf.append("origin=");
        sBuf.append(start.getLatitudeE6()/1E6);
        sBuf.append(',');
        sBuf.append(start.getLongitudeE6()/1E6);
        sBuf.append("&destination=");
        sBuf.append(dest.getLatitudeE6()/1E6);
        sBuf.append(',');
        sBuf.append(dest.getLongitudeE6()/1E6);
        sBuf.append("&sensor=true&mode=driving");
        parser = new GoogleParser(sBuf.toString());
        Route r =  parser.parse();
        return r;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}
4

3 回答 3

1

也许像这样?

public class MainActivity extends MapActivity {

    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = (MapView) findViewById(R.id.mapview); //or you can declare it directly with the API key
        // You can execute it at onCreate or whenever you want, for example: in a click event
        new AsyncRoutes().execute();
    }

    private Route directions(final GeoPoint start, final GeoPoint dest) {
        Parser parser;
        //https://developers.google.com/maps/documentation/directions/#JSON <- get api
        String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
        final StringBuffer sBuf = new StringBuffer(jsonURL);
        sBuf.append("origin=");
        sBuf.append(start.getLatitudeE6()/1E6);
        sBuf.append(',');
        sBuf.append(start.getLongitudeE6()/1E6);
        sBuf.append("&destination=");
        sBuf.append(dest.getLatitudeE6()/1E6);
        sBuf.append(',');
        sBuf.append(dest.getLongitudeE6()/1E6);
        sBuf.append("&sensor=true&mode=driving");
        parser = new GoogleParser(sBuf.toString());
        Route r =  parser.parse();
        return r;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    private class AsyncRoutes extends AsyncTask<Void, Integer, Route> {

        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {

            pDialog = new ProgressDialog(LoginActivity.this);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setMessage("Obtaining routes...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Route doInBackground(Void... params) {

            Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));
            return route;
        }

        @Override
        protected void onPostExecute(Route route) {         

            RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
            mapView.getOverlays().add(routeOverlay);
            mapView.invalidate();

            pDialog.dismiss();
        }
    }

}
于 2013-03-13T04:58:19.150 回答
0

当他们说您可以使用 AsyncTask 时,我认为他们的意思是:

public class MainActivity extends MapActivity {

private MapView mapView; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mapView = (MapView) findViewById(R.id.mapview); //or you can declare it directly with the API key
    RouteTask routeTask = new RouteTask(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6));
    routeTask.execute("");
}

private Route directions(final GeoPoint start, final GeoPoint dest) {
    Parser parser;
    //https://developers.google.com/maps/documentation/directions/#JSON <- get api
    String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(start.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(start.getLongitudeE6()/1E6);
    sBuf.append("&destination=");
    sBuf.append(dest.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(dest.getLongitudeE6()/1E6);
    sBuf.append("&sensor=true&mode=driving");
    parser = new GoogleParser(sBuf.toString());
    Route r =  parser.parse();
    return r;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

class RouteTask extends AsyncTask<String, Void, String> {
    private GeoPoint start;
    private GeoPoint dest;
    private Route route;

    public RouteTask(GeoPoint start, GeoPoint dest) {
        this.start = start;
        this.dest = dest;
    }

    protected String doInBackground(String... strings) {
        //runs the directions method in a background thread.
        route = directions(start, dest);
        return "Started";
    }

    @Override
    protected void onPostExecute(String s) {
        //Updates the UI on the main/UI thread (post execute is called on the main/UI thread).
        RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
        mapView.getOverlays().add(routeOverlay);
        mapView.invalidate();
    }
}

}

这将在后台线程上运行方向方法并在主/UI 线程上更新地图。作为所有编程语言的一般做法,您应该在绘制到屏幕的线程(在本例中为主/UI 线程)上进行 UI 工作。

这应该做你需要的。

于 2013-03-13T05:31:11.217 回答
0

这是AsyncTask的基本结构

public class TalkToServer extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... params) {
    //do your work here
        return something;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
           // do something with data here-display it or send to mainactivity
}

你可以做你的网络东西和/或你拥有的东西,directionsdoInBackground()更新UI其他方法。您也可以将该directions方法作为您的私有方法AsyncTask

然后你会从你MainActivity喜欢的地方调用它

TalkToServer networkStuff = new TalkToServer(); //could pass params to a constructor here such as context
networkStuff.execute();

如果AsyncTask不是你的内部类,那么如果你想做一些事情MainActivity,你将需要一个构造函数来接收你的上下文MainActivityUI

于 2013-03-13T04:59:47.250 回答