Reputation: 973
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.
Upvotes: 2
Views: 11879
Reputation: 973
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);
}
Upvotes: 2
Reputation: 1480
What you are making now atleast causes a deadlock.
What I normally do is creating the progressdialog as you are doing now and on the same time start another thread that in this situation waits for 1.5second and stop the progressdialog then.
For example
private Runnable waitforcompletion = new Runnable()
{
@Override
public void run()
{
while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
Thread.sleep(10);
}
progress.dismiss();
}
};
And for starting just call the above runnable after creating the progressdialog
Upvotes: 0