Java 放置ProgressDialog.Disclose方法时无法显示ProgressDialog

Java 放置ProgressDialog.Disclose方法时无法显示ProgressDialog,java,android,android-layout,progressdialog,Java,Android,Android Layout,Progressdialog,当我在代码上添加ProgressDialog.dismise()方法时,ProgressDialog无法显示 我还尝试添加Thread.sleep(),这样它的执行将休眠一段时间,这可以显示progressdialog一段时间,但这也不起作用 import android.app.ProgressDialog; import android.content.Context; import android.content.pm.ResolveInfo; import android.os.Envi

当我在代码上添加
ProgressDialog.dismise()
方法时,
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();
            }
        }
    }
}
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.pm.ResolveInfo;
导入android.os.Environment;
导入android.widget.Toast;
导入java.io.BufferedInputStream;
导入java.io.BufferedOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.List;
公共类抽取{
进行对话进行对话;
公共提取(列出应用程序、字符串publicSourceDir、字符串apkname、上下文){
progressDialog=新建progressDialog(上下文);
progressDialog.setMessage(“提取”);
progressDialog.setProgressStyle(progressDialog.STYLE_微调器);
progressDialog.setUndeterminate(true);
progressDialog.show();
BufferedInputStream BufferedInputStream=null;
BufferedOutputStream BufferedOutputStream=null;
试一试{
File File=新文件(publicSourceDir);
File file1=新文件(Environment.getExternalStorageDirectory().toString()+“/Extracted APK”);
如果(!file1.exists())
file1.mkdirs();
file1=新文件(file1.getPath()+“/”+apkname+“.apk”);
如果(!file1.exists())
file1.createNewFile();
bufferedInputStream=新bufferedInputStream(新文件输入流(文件));
bufferedOutputStream=新的bufferedOutputStream(新文件输出流(文件1));
字节[]buf=新字节[1024];
内伦;
而((len=bufferedInputStream.read(buf))>0){
bufferedOutputStream.write(buf,0,len);
}
睡眠(1000);
progressDialog.disclose();
Toast.makeText(上下文,“Apk提取”,Toast.LENGTH_LONG.show();
bufferedInputStream.close();
bufferedOutputStream.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕捉(中断异常e){
e、 printStackTrace();
}最后{
试一试{
bufferedInputStream.close();
bufferedOutputStream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}

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

因为只有在长时间运行的代码完成后,UI才会在最后更新,然后已经调用了show/Disclose。这就是为什么你只看到最后的结果:一个被驳回的对话

参见示例(此处引用: ):

做一些类似于:

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,是否希望它显示出来?那就不要放弃了..我想他需要展示它,然后在完成文件处理后放弃它