Java 暂停和恢复 AsyncTasks?(安卓)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2474584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pause and Resume AsyncTasks? (Android)
提问by Matt Swanson
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.
我有一个AsyncTask
用作我游戏的倒数计时器。当它完成倒计时,它会显示超时结束屏幕,它还会更新屏幕上显示的计时器。一切正常,除了我需要能够在按下游戏中的暂停按钮时暂停和恢复。
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.
如果我取消它并尝试重新执行它,它会以IllegalStateException
. 如果我取消它并在它的位置实例化一个新的 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
s重新启动它,或者我应该采取不同的方式来做到这一点?
EDIT:
编辑:
This is what I did for a solution:
这是我为解决方案所做的:
mhandler = new Handler();
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.
不确定这是否是最好的方法,但它对我有用。
采纳答案by CommonsWare
I have an AsyncTask that acts as a countdown timer for my game.
我有一个 AsyncTask 作为我的游戏的倒数计时器。
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
.
这不是一个很好的用法AsyncTask
。AsyncTasks
应该做工作并完成,而不是尝试闲逛。对于倒数计时器,请使用postDelayed()
-- 您甚至不需要后台线程。或者,使用CountDownTimer
.
If I cancel it and try to re-execute it, it crashes with an IllegalStateException.
如果我取消它并尝试重新执行它,它会因 IllegalStateException 崩溃。
The documentationstates: "The task can be executed only once (an exception will be thrown if a second execution is attempted.)"
该文件规定:“任务只能一次执行(如果第二试图执行一个异常将被抛出。)”
回答by miaomiao
To JaySS,
对杰斯来说,
- I don't think AsyncTask can be resumed. As to pause, I've tried to use AsyncTask.cancel(true) to stop a task which is running. After cancel I use AsyncTask.isCancelled() to check whether it's cancelled or not. It returned true; however, the AsyncTask is still running.
- 我不认为 AsyncTask 可以恢复。至于暂停,我尝试使用 AsyncTask.cancel(true) 来停止正在运行的任务。取消后我使用 AsyncTask.isCancelled() 来检查它是否被取消。它返回真;但是,AsyncTask 仍在运行。
回答by HymansOnF1re
Hi
Here is another dirty solution to pause an AsyncTask, which works for me.
But it is not really a clean code.
Do it in your class that extends AsyncTask.
嗨,
这是暂停 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();
}
}