[英]progressDialog not dismissing after progressDialog.dismiss();
[英]ProgressDialog cannot show when put ProgressDialog.dismiss method
当我在代码上添加progressDialog.dismiss()
方法时, ProgressDialog
无法显示
我还尝试添加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();
}
}
}
}
您需要将长期运行的工作放在单独的Thread或asyncTask中
因为只有在您长时间运行的代码完成后才更新UI,然后才调用show / dismiss。 那就是为什么您只看到最终结果的原因:对话框已关闭
请参见示例(引自此处: Android:ProgressDialog未显示 ):
做类似的事情:
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(); }
也是这里: 执行任务时不显示ProgressDialog
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.