How to restart an AsyncTask or avoid if RUNNING
<p>I am Developing an App that parses data from HTML. One of the data that is parsed is an integer that increases over time and rather than constantly requesting the data I would like to have a task that intervals the integer. Due to the fact that I need the value updated into a TextView I decided to go with an AsyncTask rather than a Timer.</p>

<p>My issue is I want to be able to restart my task once I know that it has finished, or if it is currently RUNNING simply avoid manipulating it.</p>

<p>Here is my AsyncTask:</p>

<pre><code>class TurnsUpdate extends AsyncTask&lt;Void, Integer, Exception&gt;
{
    protected void onPreExecute()
    {
        turns = Integer.parseInt(DATABASE.getValue("turns").replaceAll(" T", ""));
    }

    protected Exception doInBackground(Void...voids)
    {
        while (turns &lt; maxTurns)
        {
            try
            {
                Thread.sleep(sleep);
            }
            catch (InterruptedException e)
            {
                return e;
            }

            turns++;
            publishProgress(turns);
        }
        return null;
    }

    protected void onProgressUpdate(Integer... turns)
    {
        infoTurns.setText(Integer.toString(turns[0]) + " T");
    }

    protected void onPostExecute(Exception e)
    {
        vibrator.vibrate(500);
    }
}
</code></pre>

<p>My attempt to start or avoid:</p>

<pre><code>  if ((HOME.updateTurns == null || HOME.updateTurns.getStatus() == AsyncTask.Status.FINISHED) &amp;&amp; HOME.updateTurns.getStatus() != AsyncTask.Status.RUNNING)
    {
        HOME.updateTurns = (UpdateTurns) new UpdateTurns();
        HOME.updateTurns.execute();
    }
</code></pre>

<p>Am I going about this correctly?</p>
