android - Running many Asynctask on timer(countdowntimer) -
i have created countdowntimer
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
, doing operations on specific condition.
basically, checking website, after time interval(say 3 second), task has been completed or not.
now if internet runs fast, there no issue code,
if task gets completed,i cancel timer in onpostexecute , further work.
but if internet runs slow, means response doesn't come in 3 seconds, onpostexecute
called more once(executing code inside more once), while task completed, got response late due server issue/internet delay.
how can make sure code inside onpostexecute
gets called once?
possible approaches:
- take static flag variable, make true , check in others...
i looking solution, reliable, maybe android provides mechanism synchronise this.. thanks
you can check status of asynctask
, depending on current status can perform logic.
please take asynctask
object globally in 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 situation take appropriate action android.util.log.i("tag", "not null"); if(paymenttask.getstatus() == (asynctask.status.pending)) { //indicates task has not been executed yet. android.util.log.i("tag", "pending"); } else if(paymenttask.getstatus() == (asynctask.status.running)) { //indicates task running. android.util.log.i("tag", "running"); } else if(paymenttask.getstatus() == (asynctask.status.finished)) { //indicates asynctask.onpostexecute has finished. android.util.log.i("tag", "finished"); } } } }
i hope can out.
thanks.
Comments
Post a Comment