ProgressDialog
方法时无法显示 progressDialog.dismiss()
我还尝试添加Thread.sleep()
,以便执行将暂停一段时间,这可能会显示progressdialog
一段时间,但这也无效。
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();
}
}
}
}
答案 0 :(得分:1)
您需要将长时间运行的工作放在单独的Thread或asyncTask
中因为只有在长时间运行的代码完成后才更新UI,然后才会调用show / dismiss。这就是为什么你只看到最终结果:一个被解雇的对话框
参见示例(引自此处: Android: ProgressDialog doesn't show):
做类似的事情:
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(); }