1
Hello, I need to implement the `Progressdialog while the file is being extracted.
My extraction code is following. Now just implement ProgressDialog
, recalling that the downloaded code has already been tried to be implemented the ProgressDialog
for me, but it didn’t work, he gets the feather 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 smoothly, there are no errors in the extraction code, only the ProgressDialog
who does not appear.
Someone can help me find the mistake?
Thank you!
Hello friend, exactly that. The screen is freezing, give a lock exactly at the time of extraction. I’ll get more on the Asynctask class, thank you very much!
– Tech Positivo
Friend, I added an example in the answer for better understanding.
– Emerson JS
Friend, what a wonder ... Thank you very much, happy one here. But so, what if I want to perform two tasks at once, you know how it does? Ex: After extracting the file, I want to delete it. Doing both at once, is it complicated? Remembering that I already have the exclusion code, I just don’t know how to determine first to extract and then delete. Thank you so much again, you don’t know how much it helped me.
– Tech Positivo
If the two tasks can be performed in sequence, you can place everything within the same thread, calling the extraction method and in the deletion sequence, all within the doInBackground. Now, if you want to do an Asynctask for each operation you should know that the procedures will be asynchronous, so simply calling one after the other (with the method execute) in the main Activity does not guarantee that they will be executed, in practice, one after the other.
– Emerson JS