进度条未在 Android 更新

Progress bar not updating on Android

我找到了一个优秀的 UnZip class,它支持 Android 的进度条。但是,在我的应用程序中单击提取时,进度条不会更新。解压缩工作正常。

有人可以告诉我我做错了什么吗?

public class UnZip1 extends AsyncTask<Void, Integer, Integer> {

    {

    }

    private String _zipFile;   
    private String _location;
    private ProgressDialog mProgressDialog;
    private int per = 0;
    private Context _conti;
    private ProgressBar bar1;

    public UnZip1(Context conti,String zipFile, String location) {
        _conti = conti;
        _zipFile = zipFile;     
        _location = location;      
        bar1 = (ProgressBar)findViewById(R.id.pb1);
        _dirChecker("");   
    }  



    public void streamCopy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[32 * 1024]; // play with sizes..
        int readCount;
        while ((readCount = in.read(buffer)) != -1) {
            out.write(buffer, 0, readCount);

        }




    }

    protected Integer doInBackground(Void... params) {
        try  {       
            ZipFile zip = new ZipFile(_zipFile);

            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
                    Log.v("Decompress", "more " + ze.getName());

                    per++;
                    publishProgress(per);

                    FileOutputStream fout = new FileOutputStream(_location + ze.getName());

                    streamCopy(zin, fout);

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

    protected void onProgressUpdate(Integer... progress) {
       bar1.setProgress(per);
    }

    protected void onPostExecute(Integer... result) {
        Log.i("UnZip" ,"Completed. Total size: "+result);
    }

    private void _dirChecker(String dir) {     
        File f = new File(_location + dir);      
        if(!f.isDirectory()) {       
            f.mkdirs();     
        }   
    }

}
}

你能试试这个吗:

@Override
protected void onProgressUpdate(Integer... progress) {
    // TODO Auto-generated method stub
    super.onProgressUpdate(values); 
    bar1.setProgress(progress[0])
}

您必须将 ProgressDialog 与 STYLE_HORIZONTAL 一起使用 如果你想看到实际的进展。 您可以在 preExecute():

中添加初始化
@Override
protected void onPreExecute()
{
  super.onPreExecute();

  mProgressDialog = new ProgressDialog(MainActivity.this);
  mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
  {
    public void onCancel(DialogInterface dialog)
    {
    }
  });

  mProgressDialog.setCancelable(true);
  mProgressDialog.setMessage("Progress");
  mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  mProgressDialog.setProgress(1);
  mProgressDialog.show();
}

MainActivity 是我的 activity class.

的名称

然后在onProgressUpdate函数上设置进度。

protected void onProgressUpdate(Integer... progress)
{
  mProgressDialog.setProgress(progress[0]);
}