Extracting file with ProgressDialog

1

Hello, I need to implement ProgressDialog while the file is being extracted.

My extraction code is next. Now I just need to implement ProgressDialog , remembering that the code I've tried has been tried to be implemented ProgressDialog by me, but it did not work, it just gets Toast :

private Button et1;
private ProgressDialog pDialog;

...
et1 = (Button) findViewById(R.id.btn1);

pDialog = new ProgressDialog(this);
pDialog.setMessage("Extraindo arquivo...");
pDialog.setCancelable(false);

et1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v1){
        etap1();    
    }
});

...

public void etap1(){
    showpDialog(); <--

    String zipFilePath = Environment.getExternalStorageDirectory() 
    .getAbsolutePath()+"/"; 
    unpackZip(zipFilePath, "arquivo.zip"); 
} 

private boolean unpackZip(String path, String zipname) { 
    InputStream is; 
    ZipInputStream zis; 
    try {
        String filename; 
        is = new FileInputStream(path + zipname); 
        zis = new ZipInputStream(new BufferedInputStream(is)); 
        ZipEntry mZipEntry; byte[] buffer = new byte[1024]; 
        int count; 
        while ((mZipEntry = zis.getNextEntry()) != null) { 
            filename = mZipEntry.getName(); 

            if (mZipEntry.isDirectory()) { 
                File fmd = new File(path + filename); 
                fmd.mkdirs(); 
                continue; 
            } 
            FileOutputStream fout = new FileOutputStream(path + filename); 
            while ((count = zis.read(buffer)) != -1) { 
                fout.write(buffer, 0, count); 
            } 
            fout.close(); zis.closeEntry(); 
            Toast.makeText(getApplicationContext(), "Extraído com sucesso", 
                           Toast.LENGTH_SHORT).show(); 
        }

        hidepDialog(); <--
        zis.close(); 
    } catch (IOException e) { e.printStackTrace(); 
        return false; 
    } 

    hidepDialog(); <--
    return true; 
}

private void showpDialog() { <--
    if (!pDialog.isShowing())
        pDialog.show();
}

private void hidepDialog() { <--
    if (pDialog.isShowing())
        pDialog.dismiss();
}

The file is extracted without problems, there are no errors in the extraction code, only ProgressDialog does not appear.

Can anyone help me find the error?

Thank you!

    
asked by anonymous 27.05.2016 / 18:54

1 answer

1

Good afternoon, my friend.

What might be happening is that your file extraction process, running on the UI Thread ), should be damaging the screen refresh, "freezing "the screen, then your dialog is not displayed.

I suggest extracting a thread to , so you can leave the main thread in charge of displaying your dialog .

A great alternative to working with threads on Android is the use of the AsyncTask class.

All extraction logic should be in a class that extends the AsyncTask class .

In this class you can override the following methods:

  • doInBackground : here is the code responsible for extracting your file;
  • onPreExecute : here is the code that will run before the extraction process begins, for example, display the dialog here;
  • onPostExecute : in this method the code to run after the end of the process, eg hide the dialog;

More information: link

An example to help you understand better:

// classe que extrai o arquivo e estente AsyncTask
public class ExtrairTask extends AsyncTask<Void, Void, Void> {

    // variável do dialog
    private ProgressDialog pDialog;

    // construtor padrão
    public ExtrairTask() {

        // instanciando o dialog
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Extraindo arquivo...");
        pDialog.setCancelable(false);
    }

    @Override
    protected Void doInBackground(Void... params) { 
        /*
            aqui vai o código para extrair o  arquivo... 
            ele pode ser implementando aqui ou pode ser feita a chamada de uma função
        */
    }

    @Override
    protected void onPreExecute() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

}

// sua activity, onde o usuário executa a ação para extrair o arquivo
public class MainActivity extends Activity {

    // variável da sua tarefa de extração
   private ExtrairTask et;

   public void extrairArquivo(View v) {

        // em resposta a uma ação do usuário, instanciamos a tarefa e executamos
        et = new ExtrairTask
        et.execute();
   }

}

NOTE: The code was not compiled, as it was only added as an example for a better understanding.

    
27.05.2016 / 19:43