2

我有一个活动,我在其中调用一项服务,该服务可能需要一段时间才能完成,直到该服务未完成,单击的菜单选项应返回错误消息,例如“数据尚未准备好,请稍后再试” ,我想在抛出该错误之前给它几秒钟的时间来完成,在此期间,我想在屏幕上显示一个进度对话框。这是我的代码:

if(calculatedValue.equals(NOT_YET_CALCULATED)){
                //show progress dialog
                ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.wait), true);
                long timeStarted = System.currentTimeMillis();
                while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                    // wait for at most 1.5 seconds
                }
                progress.dismiss();
                if(calculatedValue.equals(NOT_YET_CALCULATED)){
                    //if it's still not there, there's probably a problem, show generic alert dialog
                    AlertDialog dialog = new AlertDialog.Builder(this).create();
                    dialog.setTitle(R.string.not_yet_calulated_alert_title);
                    dialog.setMessage(getResources().getString(R.string.not_yet_calulated_alert_message));
                    dialog.show();
                }else{
                    startActivity(j);
                }
            }else{
                startActivity(j);
            }

让我解释一下:我检查该值是否存在,如果不存在,我会显示一个进度图标(我要画圆圈)1.5 秒。如果在那之后它仍然不存在,我放弃并显示错误消息。

问题是进度的东西没有显示在屏幕上。我究竟做错了什么?

谢谢,即

4

2 回答 2

2

想通了,应该为此使用异步任务,这是代码:

    if(calculatedValue.equals(NOT_YET_CALCULATED)){
    //show progress dialog
    final ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.wait), true);
    AsyncTask<Void, Void, Boolean> waitForCompletion = new AsyncTask<Void, Void, Boolean>(){
        @Override
        protected Boolean doInBackground(Void... params) {
            long timeStarted = System.currentTimeMillis();
            while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                // wait for 1.5 ms
                 try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        Log.e(TAG, "thread interrupted", e);
                    }
            }
            progress.dismiss();
            return calculatedValue.equals(NOT_YET_CALCULATED);
        };
        @Override
        protected void onPostExecute(Boolean result) {
        if(result == true){
            AlertDialog dialog = new AlertDialog.Builder(TodayActivity.this).create();
            dialog.setTitle(R.string.not_yet_calulated_alert_title);
            dialog.setMessage(getResources().getString(R.string.not_yet_calulated_alert_message));
            dialog.show();
        }else{
            i.putExtra(Constants.AMOUNT, Integer.parseInt(calculatedValue));
            startActivity(i);
        }
        }
    };
    waitForCompletion.execute(null, null, null);
}else{
i.putExtra(Constants.AMOUNT, Integer.parseInt(calculatedValue));
startActivity(i);
}
于 2011-05-05T15:25:41.213 回答
0

你现在所做的至少会导致僵局。

我通常做的是像你现在一样创建progressdialog,同时启动另一个线程,在这种情况下等待1.5秒然后停止progressdialog。

例如

    private Runnable waitforcompletion = new Runnable()
{
    @Override
    public void run()
    {
         while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                Thread.sleep(10);
                }

                progress.dismiss(); 
    }
};

并且在创建progressdialog之后只需调用上面的runnable

于 2011-05-03T16:44:31.583 回答