1

UIスレッドでのネットワーク処理を避けるために、AsyncTaskでdirection()関数を使用する必要があります.私はこれを行うことができません. 私のコードは

    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 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置くこともできますdirectionsAsyncTask

次に、あなたの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 に答える
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 に答える