gpt4 book ai didi

java - 当放置 ProgressDialog.dismiss 方法时,ProgressDialog 无法显示

转载 作者:行者123 更新时间:2023-12-02 13:38:30 27 4
gpt4 key购买 nike

ProgressDialog 在我的代码中添加 progressDialog.dismiss() 方法时无法显示

我还尝试添加Thread.sleep(),以便它的执行将 hibernate 一段时间,这可以显示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();
}
}
}
}

最佳答案

您需要将长时间运行的工作放在单独的线程或 asyncTask 中

因为 UI 仅在长时间运行的代码完成后才会更新,然后显示/关闭已被调用。这就是为什么您只能看到最终结果:关闭的对话框

查看示例(引用自此处: 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();
}

还有这里: ProgressDialog not showing while performing a task

关于java - 当放置 ProgressDialog.dismiss 方法时,ProgressDialog 无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42866518/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com