2012-08-14 110 views
2

我試圖更新一個 progress bar解壓縮SD卡中的文件。我的解壓縮工作正常,但沒有出現progress bar。這是我在mainactivity代碼:解壓縮文件的進度條

private ProgressBar bar; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    bar = (ProgressBar) findViewById(R.id.progress); 

    String zipFilename = Environment.getExternalStorageDirectory() + "path to my zip file in sd card"; 
    String unzipLocation = Environment.getExternalStorageDirectory() + "the output folder"; 




    Decompress d = new Decompress(zipFilename, unzipLocation); 
    d.unzip(); 
} 

public class Decompress { 
    private String _zipFile; 
    private String _location; 
    private int per = 0; 



    public Decompress(String zipFile, String location) { 
     _zipFile = zipFile;  
     _location = location;  
     _dirChecker(""); 
     }  
    public void unzip() {  
     try {  
      ZipFile zip = new ZipFile(_zipFile); 
      bar.setMax(zip.size()); 
      FileInputStream fin = new FileInputStream(_zipFile);  
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null;  
      while ((ze = zin.getNextEntry()) != null) { 

       Log.v("Decompress", "Unzipping " + ze.getName());   
       if(ze.isDirectory()) {   
        _dirChecker(ze.getName());   
        } else {  
// Here I am doing the update of my progress bar 

         per++; 
         bar.setProgress(per); 
         FileOutputStream fout = new FileOutputStream(_location + ze.getName());   
         for (int 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();  
      } 
     } 

    } 
} 
+0

一切對話是好的,現在把你的在['AsyncTask']中解壓縮代碼(http://developer.android.com/reference/android/os/AsyncTask.html)。在'onPreExecute()'中將ProgressBar從'doInBackground()'更新爲'onProgressUpdate()'並關閉它'onPostExecute()'。 – user370305 2012-08-14 07:12:29

+0

看來,你正在做onCreate方法中的所有工作。雖然你應該產生一個單獨的線程解壓縮。 – harism 2012-08-14 07:13:21

+0

@ user370305讓我這樣做 – Adam 2012-08-14 07:16:36

回答

7

你解壓碼是主/ UI線程上運行,從而凍結UI。您想使用AsyncTask在後臺線程中進行解壓縮。

例如,對於你的情況:

private class Decompress extends AsyncTask<Void, Integer, Integer> { 

    private String _zipFile; 
    private String _location; 
    private int per = 0; 

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


    @Override 
    protected Integer doInBackground() { 
     try { 
      ZipFile zip = new ZipFile(_zipFile); 
      bar.setMax(zip.size()); 
      FileInputStream fin = new FileInputStream(_zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 

       Log.v("Decompress", "Unzipping " + ze.getName()); 
       if (ze.isDirectory()) { 
        _dirChecker(ze.getName()); 
       } else { 
        // Here I am doing the update of my progress bar 

        per++; 
        publishProgress(per); 

        FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
        for (int 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); 
     } 
     return totalSize; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     bar.setProgress(per); //Since it's an inner class, Bar should be able to be called directly 
    } 

    @Override  
    protected void onPostExecute(Integer... result) { 
     Log.i("Completed. Total size: " + result); 
    } 
} 
+0

我該怎麼稱呼它 – Adam 2012-08-14 08:31:15

+0

你可以用'new Decompress(「zipfile」,「location」)調用它。execute();' – 2012-08-14 09:02:44

0

的AsyncTask是一個好主意。應該是這樣的:

private class RunningAlternativSearchAlways extends 
     AsyncTask<Integer, Integer, Void> { 


    final ProgressDialog dialog = new ProgressDialog(SearchResult.this) { 
     @Override 
     public boolean onSearchRequested() { 
      return false; 
     } 
    }; 



    @Override 
    protected void onPreExecute() { 
     String DialogTitel = getString(R.string.daten_wait_titel); 
     DialogText = getString(R.string.dialog_alternativalways_text); 
     sucheNach = getString(R.string.dialog_suche_nach); 
     dialog.setCancelable(true); 
     dialog.setTitle(DialogTitel); 
     dialog.setIcon(R.drawable.icon); 
     dialog.setMessage(DialogText); 
     dialog.setOnDismissListener(new OnDismissListener() { 
      public void onDismiss(DialogInterface arg0) { 
       // TODO Auto-generated method stub 
       cancleBarcodeWorker(); 
      } 
     }); 
     dialog.show(); 
    } 

    public void cancleBarcodeWorker() { 
     try { 
      this.cancel(true); 
     } catch (Exception ex) { 

     } 
    } 

    @Override 
    protected void onCancelled() { 
     dialog.cancel(); 
     Toast toast = Toast.makeText(SearchResult.this, SearchResult.this 
       .getString(R.string.toast_suche_abgebrochen), 
       Toast.LENGTH_LONG); 
     toast.show(); 
    } 

    @Override 
    protected Void doInBackground(Integer... param) { 
     // UNZIP YOUR FILE HERE 

     // PUBLISH YOUR PROGRESS LIKE THIS 
     publishProgress(0, i); 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     switch (values[0]) { 
     case 0: 
      // Suchbegriffe einzeln analysieren 
      dialog.setMessage(DialogText + "\n" + sucheNach + " " 
        + suchBegriffe[values[1]]); 
      break; 
     } 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // CLOSE YOUR DIALOG HERE 
     dialog.cancle(); 
    } 
} 
0

在Android中,你有一個UI線程,並根據需要儘可能多的其他後臺線程。只有一個任務可以在任何時候在UI線程上完成。當你在UI線程上解壓文件時,它將阻止任何其他操作,如關於進度對話框的操作。 Android提供了一個AsyncTask,它可以輕鬆地讓您在後臺進行工作,同時也可以將更新發布到UI線程中。

嘗試使用的AsyncTask創造,建立和onPreExecute()顯示你的對話框,解壓縮文件和發佈doInBackground()的進展,onProgressUpdate()顯示更新並關閉在onPostExecute()