2

我有一个活动,我在其中调用可能需要一段时间才能完成的服务,直到该服务没有完成,点击的菜单选项应返回错误信息如“数据尚未准备好,请稍后再试” 但是,我想在抛出该错误之前给它几秒钟完成,在此期间,我想在屏幕上显示progressdialog。 这里是我的代码:在等待线程完成时显示进度对话框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秒。如果在此之后它仍然不在,我放弃并显示一条错误消息。

问题在于屏幕上没有显示进度。 我做错了什么?

谢谢, e。

回答

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); 
} 
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

+0

我改变它做到这一点,但对话框仍然不出现 只是为了澄清,我正在寻找像这样的东西:[image](http://huuah.com/images/an droiddialog/progress.png) – ekatz 2011-05-03 17:35:12

+0

你的显示progressdialog的代码似乎是正确的。你是从UI线程还是另一个运行代码的那部分? – matsjoe 2011-05-03 17:42:46

+0

我正在主线程上运行它 部分 \t“public boolean onOptionsItemSelected(MenuItem item)” method – ekatz 2011-05-03 18:34:28