Android: ProgressDialog 未在线程内进行

Android: ProgressDialog not progressing inside a Thread

我正在调用多个 AsyncTasks 来完成一项工作。为了知道他们什么时候完成。我有一个对象(同步),其分子包含当前 运行 AsyncTasks 的数量。

部署完所有这些后,我执行以下操作:

final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage(getString(R.string.please_wait));
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(false);
pd.setProgress(0);
pd.setMax(Utils.getAsyncs());
pd.show();

new Thread(new Runnable() {
    @Override
    public void run() {
        while (Utils.getAsyncs() > 0) 
            pd.setProgress(pd.getMax() - Utils.getAsyncs());
        runOnUiThread(new Runnable() {
            public void run() {
                pd.dismiss();
            }
        });
    }
}).start();

当对话框显示时,它开始进行,但在某些时候它会卡住直到一切结束然后消失(如预期的那样)。

我试过

pd.setProgress(pd.getMax() - Utils.getAsyncs());

也在 runOnUiThread 中,但这让事情变得更糟,我确定我还遗漏了其他东西。因此我的问题。谢谢

应要求编辑:

public static int getAsyncs() {
    return asyncs;
}

编辑 2:我根据评论做了以下操作

while (Utils.getAsyncs() > 0) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {}
    runOnUiThread(new Runnable() {
        public void run() {
            pd.setProgress(pd.getMax() - Utils.getAsyncs());
        }
    });                                    
}

而且好像更好

做这样的事情

    new Thread(new Runnable() {
        public void run() {
            while (prStatus < 100) {
                prStatus += 1;
                handler.post(new Runnable() {
                    public void run() {
                        pb_2.setProgress(prStatus);
                    }
                });
                try {
                    Thread.sleep(150);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(prStatus == 100)
                    prStatus = 0;
            }
        }
    }).start();

在您的 class 字段中

private Handler progressHandler = new Handler();
 private Runnable progressRunnable = new Runnable() {
        @Override
        public void run() {
            progressDialog.setProgress(progressValue);
            progressHandler.postDelayed(this, 1000);
        }
    };

耗时线程启动时

// Here start time consuming thread
// Here show the ProgressDialog    
progressHandler.postDelayed(progressRunnable, 1000);

当耗时线程结束时

 progressHandler.removeCallbacks(progressRunnable);
 /// Here dismiss the ProgressDialog.

已添加:

而不是你可能用于耗时代码的 new Thread(new Runnable) 我建议这样做:

初始化任务:

   MyTask task = new MyTask();
   task.execute();
   // Here show the PorgressDialog
   progressHandler.postDelayed(progressRunnable, 1000);

在您的主 class 中添加此私有 class:

 private class MyTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

        //Here do your time consuming work             
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
        // This will be called on the UI thread after doInBackground returns
         progressHandler.removeCallbacks(progressRunnable);
         progressDialog.dismiss();             
        }
    }