如何在 android 中控制计时器?

How can I control a timer in android?

我想做一个关于小游戏的申请。 详情:如果您不回答或答案错误,您必须在 2 秒内回答问题 -> 游戏结束。但是,如果您的回答是正确的,计时器将重置为 0,并根据不同的问题再次倒计时。

我已经在网站上看到了很多关于计时器的代码,但我对它的理解不是很清楚:(

所以我想问:我怎样才能设置一个计时器 运行 只有 2 秒,我怎样才能重置它并继续一个新问题?

请帮帮我。

您可以使用处理程序。

Handler h = new Handler();
h.postDelayed(new Runnable() {
    @Override
    public void run() {
        //this will happen after 2000 ms
    }
}, 2000);

也许这可以帮到你:

Handler handler = new Handler();
handler.post(new Runnable() {

@Override
public void run() {
    // FIRE GAME OVER
    handler.postDelayed(this, 2000); // set time here to refresh textView
}
});
  • 您可以在 2000 毫秒后结束游戏。
  • 如果您答对了问题 -> 从处理程序中删除回调并在下一个问题开始时重置它。

我们想使用定时器 class。

private Timer timer;

当您准备好让计时器开始计时时——假设是在您按下某个按钮之后——执行此操作以启动它:

timer = new Timer();
timer.scheduleAtFixedRate(incrementTime(), 0, 100);

第一行是我们创建一个新的定时器。相当标准。然而,第二行是我想让你看到的。

incrementTime() 是一种在每个 "tick" 时钟结束时调用的方法。您可以随意调用此方法,但它必须 return TimerTask 的一个实例。如果你愿意,你甚至可以创建一个匿名界面,但我更喜欢将它移到它自己的代码部分。

0 是我们的起始位置。我们从这里开始数。简单。

100 是 "tick" 时钟的大小(以毫秒为单位)。在这里,它是每 100 毫秒,或每 1/10 秒。我在编写这段代码时使用了这个值,因为我正在制作一个秒表应用程序,我希望我的时钟每 0.1 秒更改一次。

至于你的项目,我建议将计时器的任务作为你的问题切换方法。使其每 2000 毫秒或 2 秒发生一次。

我用 Thread/Runnable 做了类似的事情。

Thread t = new Thread(new Runnable() {

    public void run() {
        final long startTime = getTime();
        final long maxEndTime = startTime + 2000L;
        try {
            while (shouldContinueWaiting()) {
                if (getTime() > maxEndTime) {
                    throw new TimeoutException();
                }
                sleep();
            }
        } catch (InterruptedException e) {
            handleInterrupt();
        } catch (TimeoutException e) {
            handleTimeout();
        }
    }


    boolean shouldContinueWaiting() {
        // Has the user already answered?
    }

    void handleInterrupt() {
        // The user has answered. Dispose of this thread.
    }

    void handleTimeout() {
        // User didn't answer in time
    }

    void sleep() throws InterruptedException {
        Thread.sleep(SLEEP_DURATION_IN_MILLIS);
    }

    void getTime() {
         return System.currentTimeMillis();
    }

然后您可以 start/restart 线程:

t = new Thread(same as above...);
t.start();

并停止:

t.interrupt();

您可以像这样在 android 中使用 CountDownTimer:

 public class Myclass {
      myTimer timer =new myTimer(2000,1000);
     public void creatQuestion(){
         timer.start();
        //method you init question and show it to user
     }
     public void getUserAnswer(/*evry thing you expected*/)
     {
         //if answer is true call timer.start()
        //else call  timer.onFinish(); to run onfinish in timer
     }
    public class myTimer extends CountDownTimer {
          public myTimer(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
          }

          @Override
          public void onTick(long millisUntilFinished) {
               // you can update ui here
          }

         @Override
         public void onFinish() {
             this.cancel();
             //fire game over event
           }
    }

}

希望能让你满意