2013-12-14 62 views
0

我想要显示对话框进度,当我提取的东西。但我的代码做的工作,但它并没有更新的进展:(这是我的代码Asynctask没有更新进度

主代码

class Install_Web extends AsyncTask<String, Integer, Void> { 

       /** 
       * Before starting background thread Show Progress Bar Dialog 
       * */ 
       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        showDialog(progress_bar_type); 
       } 

       @Override 
       protected Void doInBackground(String... params) { 
        try { 
         copyAssets(); 
         String zipFile = server_Runner.getHttpDirectory() + "/www/www (2).zip"; 
         String unzipLocation = server_Runner.getHttpDirectory() + "/"; 

         Decompress d = new Decompress(zipFile, unzipLocation); 
         d.unzip(); 

         publishProgress((int)((ze.getName().length()*100)/ze.getSize())); 
} 

        catch (Exception e) { 
        } 
        return null; 
       } 

       @Override 
       protected void onProgressUpdate(Integer... m) { 
        pDialog.setProgress((m[0])); 
       } 

       /** 
       * After completing background task 
       * Dismiss the progress dialog 
       * **/ 
       @Override 
       protected void onPostExecute(Void res) { 

        dismissDialog(progress_bar_type); 


        } 
      } 

进度对话框

@Override 
      protected Dialog onCreateDialog(int id) { 
       switch (id) { 

       case progress_bar_type : 
        // we set this to 0 
        pDialog = new ProgressDialog(this); 
        pDialog.setMessage("Installing file. Please wait..."); 
        pDialog.setIndeterminate(false); 
        pDialog.setMax(100); 
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
        pDialog.setCancelable(true); 
        pDialog.show(); 
        return pDialog; 
       default: 
        return null; 
       } 
      } 

提取代码:

public class Decompress { 
      private String _zipFile; 
      private String _location; 

      public Decompress(String zipFile, String location) { 
      _zipFile = zipFile; 
      _location = location; 

      _dirChecker(""); 
      } 

      public void unzip() { 
      try { 
       FileInputStream fin = new FileInputStream(_zipFile); 
       ZipInputStream zin = new ZipInputStream(fin); 
       while ((ze = zin.getNextEntry()) != null) { 
       Log.v("Decompress", "Unzipping " + ze.getName()); 

       if(ze.isDirectory()) { 
        _dirChecker(ze.getName()); 
       } else { 
        FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
        for (c = zin.read(); c != -1; c = zin.read()) { 
        fout.write(c); 
        } 

        zin.closeEntry(); 
        fout.close(); 
       } 

       } 
       zin.close(); 
      } catch(Exception e) { 
       Log.e("Decompress", "unzip", e); 
      } 





      } 

      private void _dirChecker(String dir) { 
      File f = new File(_location + dir); 

      if(!f.isDirectory()) { 
       f.mkdirs(); 
      } 
      } 
     } 

我没有发布与进度无关的代码.. okkk

回答

-1

解压缩操作是同步的,所以即使在代码到达publishProgress调用时您正在后台线程上运行,您已经完成了该操作,因此它只会发布100%。

我认为你需要重构你的代码,以便在你的unzip()函数中调用publishProgress while循环的每一次迭代,或者类似的东西。

+0

我需要做什么? – user3098412

+0

您需要发布进度更新,而不是在完成之后。我将更改解压缩函数,以便您可以在while循环内调用publishProgress。 – ksasq