Show progressDialog for 2 seconds while waiting for a thread to finish
figured it out, should use async task for this, here's the code:
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);
}
ekatz
Updated on June 04, 2022Comments
-
ekatz almost 2 years
I have an activity in which I call a service that can take a while to complete and until that service didn't finish, the menu options that are clicked should return an error message like "data not yet ready, please try again soon" however, I want to give it a couple of seconds to finish before I throw that error and during that time, I want to show a progressdialog on the screen. here is the code that I have:
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); }
Let me explain: I check if the value exists, and if it doesn't I show a progress icon (i'm going for the circle thingy) for 1.5 seconds. If after that time it's still not there, I give up and show an error message.
The problem is that the progress thing does not show up on the screen. what am I doing wrong?
thanks, e.
-
ekatz about 13 yearsI changed it to do that but the dialog still doesn't appear just to clarify, I'm looking for something that looks like this: image
-
matsjoe about 13 yearsYour code for showing the progressdialog seems to be correct. Are you running that part of code from the UI thread or another one?
-
ekatz about 13 yearsI'm running it on the main thread part of the "public boolean onOptionsItemSelected(MenuItem item)" method