Reputation: 185
ProgressDialog
cannot show when i add progressDialog.dismiss()
method on my code
I also try to add Thread.sleep()
so that it execution will sleep for some time which can show progressdialog
for some time but this will also not work.
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ResolveInfo;
import android.os.Environment;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class Extraction {
ProgressDialog progressDialog;
public Extraction(List<ResolveInfo> apps, String publicSourceDir, String apkname, Context context) {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Extracting");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setIndeterminate(true);
progressDialog.show();
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
File file = new File(publicSourceDir);
File file1 = new File(Environment.getExternalStorageDirectory().toString() + "/Extracted APK");
if (!file1.exists())
file1.mkdirs();
file1 = new File(file1.getPath() + "/" + apkname + ".apk");
if (!file1.exists())
file1.createNewFile();
bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file1));
byte[] buf = new byte[1024];
int len;
while ((len = bufferedInputStream.read(buf)) > 0) {
bufferedOutputStream.write(buf, 0, len);
}
Thread.sleep(1000);
progressDialog.dismiss();
Toast.makeText(context, "Apk Extracted", Toast.LENGTH_LONG).show();
bufferedInputStream.close();
bufferedOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
bufferedInputStream.close();
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Upvotes: 0
Views: 175
Reputation: 1539
your need to put your long running work in a separate Thread or asyncTask
Because the UI is only updated at the end when your long running code is completed and then the show/dismiss is already called. thats why you only see the final result: a dismissed dialog
see the example (quoted from here: Android: ProgressDialog doesn't show ):
Do something like:
public void doBackup(View view) throws IOException{ final ProgressDialog pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); pd.setMessage("Running backup. Do not unplug drive"); pd.setIndeterminate(true); pd.setCancelable(false); pd.show(); Thread mThread = new Thread() { @Override public void run() { File source = new File("/mnt/extSdCard/DirectEnquiries"); File dest = new File("/mnt/UsbDriveA/Backup"); copyDirectory(source, dest); pd.dismiss(); } }; mThread.start(); }
and also here: ProgressDialog not showing while performing a task
Upvotes: 1