暂停和恢复AsyncTasks? (安卓) [英] Pause and Resume AsyncTasks? (Android)

查看:714
本文介绍了暂停和恢复AsyncTasks? (安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的AsyncTask 是作为一个倒数计时器为我的比赛。当它完成倒计时它显示出时间结束画面,它也更新显示在屏​​幕上的计时器。一切工作正常,但我需要能够暂停和恢复本当在游戏中的暂停按钮是pressed。

如果我取消它,并尝试重新执行它,它有一个 IllegalStateException异常崩溃。 如果我取消和实例在其位置一个新的AsyncTask旧再次开始运行,并且新的运行在同一时间。

有没有办法来取消/暂停计时器并使用的AsyncTask s或是否有不同的方式,我应该去这样做?

重新启动

编辑:

这是我做的一个解决方案:

mhandler =新的处理程序();

  mtimerTask =新的Runnable(){
                公共无效的run(){
                    mtimer  -  = 1;
                    如果(mtimer == 0)
                    {
                        mhandler.removeCallbacks(本);
                    }
                    mhandler.postDelayed(这一点,1000);
                }
        };
        mhandler.removeCallbacks(mtimerTask);
        mhandler.post(_timerTask)`
 

不知道,如果是去了解它的最佳方式,但它为我工作。

解决方案
  

我有一个AsyncTask的充当   倒数计时器为我的比赛。

这是不是一个好的使用的AsyncTask AsyncTasks 应该做的工作要做,不要尝试游逛。对于一个倒数计时器,使用 postDelayed() - 你甚至都不需要后台线程。或者,使用<一个href="http://developer.android.com/reference/android/os/CountDownTimer.html"><$c$c>CountDownTimer.

  

如果我取消它,并尝试重新执行   它,它有崩溃   IllegalStateException异常。

借助文档指出:该任务可以只执行一次(异常会抛出如果第二执行尝试。)

I have an AsyncTask 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.

If I cancel it and try to re-execute it, it crashes with an 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.

Is there a way to cancel/pause the timer and restart it using AsyncTasks or is there a different way I should be going about doing this?

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.

That is not a good use of AsyncTask. AsyncTasks should do work and be done, not try hanging around. For a countdown timer, use postDelayed() -- you don't even need a background thread. Or, use CountDownTimer.

If I cancel it and try to re-execute it, it crashes with an IllegalStateException.

The documentation states: "The task can be executed only once (an exception will be thrown if a second execution is attempted.)"

这篇关于暂停和恢复AsyncTasks? (安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆