我有一個活動,我在其中調用可能需要一段時間才能完成的服務,直到該服務沒有完成,點擊的菜單選項應返回錯誤信息如「數據尚未準備好,請稍後再試」 但是,我想在拋出該錯誤之前給它幾秒鐘完成,在此期間,我想在屏幕上顯示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。
我改變它做到這一點,但對話框仍然不出現 只是爲了澄清,我正在尋找像這樣的東西:[image](http://huuah.com/images/an droiddialog/progress.png) – ekatz 2011-05-03 17:35:12
你的顯示progressdialog的代碼似乎是正確的。你是從UI線程還是另一個運行代碼的那部分? – matsjoe 2011-05-03 17:42:46
我正在主線程上運行它 部分 \t「public boolean onOptionsItemSelected(MenuItem item)」 method – ekatz 2011-05-03 18:34:28