2013-12-14 61 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