2015-05-20 5 views
-1

Я нашел отличный класс UnZip с поддержкой индикаторов хода выполнения для Android. Однако индикатор выполнения не обновляется при нажатии extract в моем приложении. Раскатка работает отлично.Прогресс-бар, не обновляющийся на 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();  
     } 
    } 

} 
} 

ответ

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 это имя моего класса активности.

Затем установите прогресс в функции onProgressUpdate.

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

Вы можете попробовать это:

@Override 
protected void onProgressUpdate(Integer... progress) { 
    // TODO Auto-generated method stub 
    super.onProgressUpdate(values); 
    bar1.setProgress(progress[0]) 
} 
Смежные вопросы