简体   繁体   English

暂停并恢复AsyncTasks? (Android)

[英]Pause and Resume AsyncTasks? (Android)

I have an AsyncTask that acts as a countdown timer for my game. 我有一个AsyncTask ,它充当游戏的倒数计时器。 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. 一切正常,除了在按下游戏中的暂停按钮时我需要能够暂停并继续执行此操作。

If I cancel it and try to re-execute it, it crashes with an IllegalStateException . 如果我取消它并尝试重新执行它,它将崩溃,并IllegalStateException 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. 如果我取消它并在其位置实例化一个新的AsyncTask,则旧的将再次运行,而新的AsyncTask将同时运行。

Is there a way to cancel/pause the timer and restart it using AsyncTask s or is there a different way I should be going about doing this? 是否可以使用AsyncTask取消/暂停计时器并重新启动计时器,或者我应该采用其他方式执行此操作?

EDIT: 编辑:

This is what I did for a solution: 这是我为解决方案所做的:

mhandler = new Handler();

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

Not sure if it's the best way to go about it, but it worked for me. 不知道这是否是最好的解决方法,但这对我有用。

I have an AsyncTask that acts as a countdown timer for my game. 我有一个AsyncTask,它充当游戏的倒数计时器。

That is not a good use of AsyncTask . 那不是AsyncTask的好用法。 AsyncTasks should do work and be done, not try hanging around. AsyncTasks应该可以完成工作,而不是闲逛。 For a countdown timer, use postDelayed() -- you don't even need a background thread. 对于倒数计时器,请使用postDelayed() -您甚至不需要后台线程。 Or, use CountDownTimer . 或者,使用CountDownTimer

If I cancel it and try to re-execute it, it crashes with an IllegalStateException. 如果我取消它并尝试重新执行它,它会崩溃并抛出IllegalStateException。

The documentation states: "The task can be executed only once (an exception will be thrown if a second execution is attempted.)" 文档指出:“该任务只能执行一次(如果尝试第二次执行,则将引发异常。)”

Hi 你好
Here is another dirty solution to pause an AsyncTask, which works for me. 这是暂停AsyncTask的另一个肮脏的解决方案,对我有用。
But it is not really a clean code. 但这并不是真正的干净代码。 Do it in your class that extends AsyncTask. 在扩展AsyncTask的类中执行此操作。

public void pause()
    {
        this.isPaused = true;
    }

    public void resume()
    {
        this.isPaused = false;
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        while(!isCancelled())
        {
            while(isPaused)
                sleep(5000);

//...

private void sleep(long sleepDuration)
    {
        try
        {
            Thread.sleep(sleepDuration);
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }

To JaySS, 对于JaySS,

  1. I don't think AsyncTask can be resumed. 我认为AsyncTask无法恢复。 As to pause, I've tried to use AsyncTask.cancel(true) to stop a task which is running. 至于暂停,我尝试使用AsyncTask.cancel(true)来停止正在运行的任务。 After cancel I use AsyncTask.isCancelled() to check whether it's cancelled or not. 取消后,我使用AsyncTask.isCancelled()来检查它是否被取消。 It returned true; 它返回true; however, the AsyncTask is still running. 但是,AsyncTask仍在运行。

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

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