ProgressDialog在异步任务中冻结

时间:2013-10-24 08:07:37

标签: android dialog android-asynctask freeze

我正在从文件中读取lat / lon,创建多边形并将其添加到AsyncTask中的地图中。一切都很好 - 除了progressDialog总是在行动开始时冻结,并在行动结束后被解雇。这是我的代码:

更新后的代码:

   private class shpLoading extends AsyncTask<GoogleMap, List<LatLng>, String> {
              private ProgressDialog dialog;
              private String path; 
                      private int loaded;
              public void setPath(String p){
                     this.path = p;   
              }

              @Override
              protected String doInBackground(GoogleMap... params) {    

                    final ShpReader shpRead = new ShpReader();



                        try {
                            shpRead.read(path);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (InvalidShapeFileException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                          dialog.setMax(shpRead.daugiakampiai().size());
                          int i = 0;
                          for(List<LatLng> a: shpRead.polygons()){                                                            
                              function.arAntLinijos(a);
                              for(int h = 0; h < a.size()-1; h++){
                                  LatLng point = function.midPoint(a.get(h), a.get(h+1));
                                  a.add(h+1, point);
                                  h++; 

                                  if(i > 3000){
                                      message("You reached max count!");
                                      break;
                                  }
                              }
                              publishProgress(a);

                              i++;                           
                          }

                    return "Done";
              } 
                  @Override
              protected void onPreExecute() {                                                                         
                      dialog = new ProgressDialog(Measuring.this);   
                      dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
                      dialog.setMessage(getString(R.string.loading));
                      dialog.setCancelable(false);
                      dialog.setProgress(0);                         
                      dialog.show();
              }                                       
              }

              @Override
              protected void onProgressUpdate(List<LatLng>... values) {
                            dialog.setProgress(loaded++);
                Polygon daug = mMap.addPolygon(new PolygonOptions()
                  .addAll(values[0])
                  .strokeWidth(1)
                  .strokeColor(Color.RED)
                  .fillColor(0x3F00FF00));

                  plotai.add(new Polygons(db.getMaxFieldID()+1, daug)); 
                  daug = null;                               
                }

              @Override
              protected void onPostExecute(String result) {
                    dialog.dismiss();

              }                      
       } 

1 个答案:

答案 0 :(得分:2)

因为您应该使用onProgressUpdate方法而不是doInBackground方法绘制多边形。在onProgressUpdate方法中,您必须更新ProgressDialog对象中的进度(在代码ProgressDialog中显示,但不会更新其进度)。

相关问题