0

我在onPreExecte()方法的Asynctask类中使用进度对话框,并在google制作地图路线时解除onPostExecute中的对话框。我的问题是对话框在2-3秒后停止,但我的后台进程仍在工作。Android ProgressDialog在任务完成之前停止旋转

private class ParserTask extends 
      AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { 

    @Override 
    protected void onPreExecute() { 
     dialog = new ProgressDialog(MapaViagem.this); 
     // setup your dialog here 
     dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     dialog.setTitle("Traçando Rotas"); 
     dialog.setMessage("Aguarde..."); 
     dialog.setCancelable(false); 
     dialog.show(); 

    } 

     @Override 
     protected List<List<HashMap<String, String>>> doInBackground(
       String... jsonData) { 


      JSONObject jObject; 
      List<List<HashMap<String, String>>> routes = null; 

      try { 
       jObject = new JSONObject(jsonData[0]); 
       PathJSONParser parser = new PathJSONParser(); 
       routes = parser.parse(jObject); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return routes; 
     } 

     @Override 
     protected void onPostExecute(List<List<HashMap<String, String>>> routes) { 
      ArrayList<LatLng> points = null; 
      PolylineOptions polyLineOptions = null; 

      // traversing through routes 
      for (int i = 0; i < routes.size(); i++) { 
       points = new ArrayList<LatLng>(); 
       polyLineOptions = new PolylineOptions(); 
       List<HashMap<String, String>> path = routes.get(i); 

       for (int j = 0; j < path.size(); j++) { 
        HashMap<String, String> point = path.get(j); 

        double lat = Double.parseDouble(point.get("lat")); 
        double lng = Double.parseDouble(point.get("lng")); 
        LatLng position = new LatLng(lat, lng); 

        points.add(position); 
       } 

       polyLineOptions.addAll(points); 
       polyLineOptions.width(4); 
       polyLineOptions.color(Color.BLUE); 
      } 

      googleMap.addPolyline(polyLineOptions); 
      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
     } 
    } 
+0

对不起,但你是否在任务内运行任务?你为什么需要这个? – TheRedFox 2014-12-09 13:02:46

+0

@TheRedFox对不起,我编辑了这个问题 – 2014-12-09 13:04:52

+0

你怎么知道这个任务仍在运行?你可以说得更详细点吗? – TheRedFox 2014-12-09 13:09:25

回答

0

你必须把所有的“处理。第”代码doInBackground内,这意味着你有onPostExcecute内的代码。只有修改用户界面的东西应该在onPostExcecute(例如向用户显示一条消息,更改TextView的文本等),否则您将使用户界面陷入困境。 我有同样的问题,并以这种方式解决它。

希望它有帮助

相关问题