Reputation: 1441
I have created a CountDownTimer
like this
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
//timeout
}
@Override
public void onTick(long millisUntilFinished) {
PaymentAsyncTask paymentTask = new PaymentAsyncTask(this);
paymentTask.execute();
}
}
in onPostExecute
of paymentTask
, i am doing some operations on some specific condition.
Basically, i am checking from some website, after some time interval(say 3 second), task has been completed or not.
Now if internet runs fast, there is no issue with this code,
if task gets completed,i cancel the timer in onPostExecute and do my further work.
But if internet runs slow, which means response doesn't come in 3 seconds,
onPostExecute
is called more than once(executing code inside it more than once), while the task was already completed, its just that i got the response late due to server issue/internet delay.
How i can make sure that code inside onPostExecute
gets called only once?
Possible approaches:
I am looking for some solution, which is reliable, maybe android provides some mechanism to synchronise this.. Thanks
Upvotes: 0
Views: 1486
Reputation:
You can actually check the status of the AsyncTask
and depending on the current status you can perform logic.
Please take your AsyncTask
object Globally in your Activity
class.
Code Snippet:
public class MyCountDownTimer extends CountDownTimer {
PaymentAsyncTask paymentTask = null;
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
//timeout
}
@Override
public void onTick(long millisUntilFinished) {
if(paymentTask == null) {
android.util.Log.i("TAG", "Null");
paymentTask = new PaymentAsyncTask(this);
paymentTask.execute();
} else {
//Depending on your situation take appropriate action
android.util.Log.i("TAG", "Not Null");
if(paymentTask.getStatus() == (AsyncTask.Status.PENDING)) {
//Indicates that the task has not been executed yet.
android.util.Log.i("TAG", "Pending");
} else if(paymentTask.getStatus() == (AsyncTask.Status.RUNNING)) {
//Indicates that the task is running.
android.util.Log.i("TAG", "Running");
} else if(paymentTask.getStatus() == (AsyncTask.Status.FINISHED)) {
//Indicates that AsyncTask.onPostExecute has finished.
android.util.Log.i("TAG", "Finished");
}
}
}
}
I hope this can help you out.
Thanks.
Upvotes: 1
Reputation: 4139
please check its helpful for you as per http://daniel-codes.blogspot.in/...
Is Your AsyncTask Running?
A short note about AsyncTask: Prior to 4.0 (and maybe prior to 3.0, but I haven't tested), AsyncTask.getStatus() might lie to you. If the AsyncTask is canceled, it won't set the status correctly; instead, it will remain RUNNING far after AsyncTask.onCancelled() finishes.
My initial thought was to use AsyncTask.isCancelled(), but you can run into some concurrency issues there if you're trying to gauge whether the AsyncTask is done from another thread. A cancelled AsyncTask doesn't necessarily end the moment you cancel it; in fact, if you're not checking isCancelled() regularly in doInBackground() then you can end up having the AsyncTask run for a while after you cancel.
My solution is to set a boolean at the end of onCancelled() that will indicate to the system that you got to the end of execution. Here's an example of writing an AsyncTask where you can properly know when it's been finished:
private class MyAsyncTask extends AsyncTask {
private boolean mFinishedCancel = false;
protected Void doInBackground(Void... params) {
return null; // You'd normally do something here
}
protected void onCancelled() {
mFinishedCancel = true;
}
public boolean isFinished() {
return getStatus() == Status.FINISHED || mFinishedCancel;
}
}
Upvotes: 0