Pause and Resume AsyncTasks? (Android)
<p>I have an <code>AsyncTask</code> that acts as a countdown timer for my game.  When it completes the countdown it displays the out of time end screen and it also updates the timer displayed on the screen.  Everything works fine, except I need to be able to pause and resume this when the pause button in the game is pressed.</p>

<p>If I cancel it and try to re-execute it, it crashes with an <code>IllegalStateException</code>.
If I cancel it and instantiate a new AsyncTask in its place the old one begins to run again and the new one runs at the same time.</p>

<p>Is there a way to cancel/pause the timer and restart it using <code>AsyncTask</code>s or is there a different way I should be going about doing this?</p>

<p>EDIT:</p>

<p>This is what I did for a solution:</p>

<p><code>mhandler = new Handler();</code></p>

<pre><code>        mtimerTask = new Runnable(){
                public void run(){
                    mtimer -= 1;
                    if(mtimer == 0)
                    {
                        mhandler.removeCallbacks(this);
                    }
                    mhandler.postDelayed(this, 1000);
                }
        };
        mhandler.removeCallbacks(mtimerTask);
        mhandler.post(_timerTask)`
</code></pre>

<p>Not sure if it's the best way to go about it, but it worked for me.</p>
