how to do functions in an AsyncTask?

前端 未结 3 935
名媛妹妹
名媛妹妹 2021-01-23 06:59

I have to use directions() function in an AsyncTask to avoid network processing on the UI thread.i\'m unable to do this thing. MY code is

    public class MainAc         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-23 07:51

    Here is what I think they mean when they say you can use an 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 {
        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();
        }
    }
    

    }

    This will run the directions method on a background thread and update the map on the main/UI thread. AS a general practice for all programing languages you should do UI work on the thread that draws to the screen (main/UI thread in this case).

    This should do what you need it to.

提交回复
热议问题