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