[英]Android Countdown Timer Thread.sleep();
I am trying to create a circuit timer using the countdown timer. 我正在尝试使用倒数计时器创建电路计时器。 When the thread is sleeping I want the text to display "Change!"
当线程处于睡眠状态时,我希望文本显示“ Change!”。 and this to display for 5 seconds before the countdown timer starts again.
并在倒数计时器再次启动前显示5秒钟。 I am unsure how to get this working.
我不确定如何使它工作。 I know the thread is sleeping and so wont display the "Change!"
我知道线程正在休眠,因此不会显示“更改!” until after it has woke up again after the 5 seconds.
直到5秒钟后又醒来。 But I cannot understand how to get it working the way I wish it to.
但是我不明白如何按照我希望的方式工作。 Can anyone help me solve this issue?
谁能帮我解决这个问题?
public void onFinish()
{
// Text to be displayed when thread is sleeping
time.setText("Change!");
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (numberOfCircuits != 15)
{
start();
numberOfCircuits++;
}
else
{
countDownTimer.cancel();
}
}
Instead of making the main thread sleep(you are likely to get an ANR dialog), you can use a Timer which can notify you of your 5sec lapse and you can schedule your CountDownTimer again. 不用让主线程进入睡眠状态(您可能会看到一个ANR对话框),而是可以使用一个计时器,该计时器可以将5秒钟的延迟通知您,并且您可以再次安排CountDownTimer。
public void onFinish()
{
// Text to be displayed when thread is sleeping
time.setText("Change!");
new Timer("Sleeper").schedule(new TimerTask() {
@Override
public void run() {
if (numberOfCircuits != 15)
{
start();
numberOfCircuits++;
}
else
{
countDownTimer.cancel();
}
}
}, 5000); // 5 sec delay
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.