java - Android ProgressDialog 未显示(被其他线程中的代码阻止)

标签 java android multithreading

寻找类似的问题,每个人都解决了不出现进度对话框的问题,将中间代码放在单独的线程中。

我的问题是上述解决方案对我不起作用。

在我的 Activity 中:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.dialog_ddbb_download_text)
                .setPositiveButton(R.string.Accept, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // In this method I show the progress dialog
                        showProgressAndDownloadDDBB();
                    }
                })
                .setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        // Create the AlertDialog object and return it
        builder.create().show();

Activity 中的方法:

private void showProgressAndDownloadDDBB() {
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setCancelable(false);
    progressDialog.setIndeterminate(true);
    progressDialog.show();
    // Here I call the Runnable to execute the code in other Thread and let the UI draw the Progress Dialog. If it wasn't called, the progress dialog does appear.
    DDBB_Download_Manager ddbb_download_manager = new DDBB_Download_Manager(mContext, progressDialog);
    ddbb_download_manager.run();
}

我的可运行类,预计在单独的线程中运行中间代码:

public class DDBB_Download_Manager implements Runnable {


public DDBB_Download_Manager(Context context, ProgressDialog progressDialog) {
    this.mContext = context;
    this.mProgresDialog = progressDialog;
}

@Override
public void run() {
    someCode()
    Thread.sleep(3000);
    // The GUI shows the accept Button clicked for 3 seconds (like it was freezed)
    // Here I try to hide the Progress dialog after finishing the job, but it doesn't matter becasuse the progress dialog didn't even show up.
    View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);
    rootView.post(new Runnable() {
        @Override
        public void run() {
            mProgresDialog.dismiss();
        }
    });
}

所以问题是: 如果我在与 UI 线程不同的线程中执行进度对话框的 Show 和 Dismiss 方法之间的代码,为什么对话框不显示?

最佳答案

事实上,如果我不调用 Runnable,它就会出现。

这是因为当您调用 ddbb_download_manager.run() 时,您直接从可运行对象中运行 dismiss() 方法,其中进度对话框已清除/完成,并且如果如果您没有调用它,那么将显示进度对话框,因为关闭尚未被调用。

当您希望关闭进度对话框时,请确保调用ddbb_download_manager.run()。显示进度对话框后不要直接调用它。

private void showProgressAndDownloadDDBB() {
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setCancelable(false);
    progressDialog.setIndeterminate(true);
    progressDialog.show();
    // Here I call the Runnable to execute the code in other Thread and let the UI draw the Progress Dialog. If it wasn't called, the progress dialog does appear.
    DDBB_Download_Manager ddbb_download_manager = new DDBB_Download_Manager(mContext, progressDialog);
    Handler handler = new Handler();
    handler.postDelayed(ddbb_download_manager ,3*1000);
}

关于java - Android ProgressDialog 未显示(被其他线程中的代码阻止),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29996601/

相关文章:

java - Spring 和 Soap API 错误 : "Unable to validate using XSD: Your JAXP provider"

android - 尚未输入任何内容时禁用键盘的发送按钮 [Android]

android - 从数据库填充 String 和 ListView

java - 未使用 log4j.properties 文件中提供的日志模式

java - NoSuchMethodError : akka. actor.LocalActorRefProvider.log()Lakka/event/LoggingAdapter

python - 我无法理解 python 中的轮询/选择

c - 停止单个线程

multithreading - 在其他线程内部生成线程时借用的问题

java - 我需要在java中制作一个有两个选择的下拉框

android - 如何处理 webview 中的错误?