简体   繁体   English

在计时器上运行许多Asynctask(countdowntimer)

[英]Running many Asynctask on timer(countdowntimer)

I have created a CountDownTimer like this 我已经创建了一个这样的CountDownTimer

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. onPostExecutepaymentTask ,我正在某些特定条件下执行一些操作。

Basically, i am checking from some website, after some time interval(say 3 second), task has been completed or not. 基本上,我是从某个网站检查,经过一段时间(例如3秒)后,任务是否完成。

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. 如果任务完成,我取消onPostExecute中的计时器并做进一步的工作。

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. 但是,如果互联网运行缓慢 ,这意味着响应不会在3秒钟内到来, onPostExecute被调用多次( 在其中执行代码不止一次 ),而任务已经完成了,只是由于服务器问题/互联网延迟。

How i can make sure that code inside onPostExecute gets called only once? 我如何确保onPostExecute内的代码onPostExecute被调用一次?

Possible approaches: 可能的方法:

  1. Take some static flag variable, make it true and check in others... 采取一些静态标志变量,使其为真,然后签入其他变量...

I am looking for some solution, which is reliable, maybe android provides some mechanism to synchronise this.. Thanks 我正在寻找一些可靠的解决方案,也许android提供了一些机制来同步它。

You can actually check the status of the AsyncTask and depending on the current status you can perform logic. 您实际上可以检查AsyncTask的状态,并可以根据当前状态执行逻辑。

Please take your AsyncTask object Globally in your Activity class. 请在Activity类中全局使用AsyncTask对象。

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. 谢谢。

please check its helpful for you as per http://daniel-codes.blogspot.in/ ... 请根据http://daniel-codes.blogspot.in/检查其对您的帮助...

Is Your AsyncTask Running? 您的AsyncTask正在运行吗?

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. 关于AsyncTask的简短说明:在4.0之前(可能在3.0之前,但我尚未测试), AsyncTask.getStatus()可能对您说谎。 If the AsyncTask is canceled, it won't set the status correctly; 如果取消了AsyncTask,它将无法正确设置状态。 instead, it will remain RUNNING far after AsyncTask.onCancelled() finishes. 相反,它将在AsyncTask.onCancelled()完成之后保持RUNNING状态。

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. 我最初的想法是使用AsyncTask.isCancelled() ,但是如果您要评估AsyncTask是否由另一个线程完成,则可能会遇到一些并发问题。 A cancelled AsyncTask doesn't necessarily end the moment you cancel it; 被取消的AsyncTask不一定在您取消它的那一刻就结束。 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. 其实,如果你不检查()定期doInBackground isCancelled(),那么你可以最终不得不在一段时间内的AsyncTask来看,你取消后。

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. 我的解决方案是在onCancelled()的末尾设置一个布尔值,该布尔值将向系统指示您已结束执行。 Here's an example of writing an AsyncTask where you can properly know when it's been finished: 这是编写AsyncTask的示例,您可以在其中正确地知道何时完成:

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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM