可以在文本游戏中暂停的计时器

时间:2016-12-09 11:16:56

标签: java android timer

我需要一个可以在文字游戏中暂停的计时器。目前使用CountDowntimer。什么是最好的方式来解决这个问题?

2 个答案:

答案 0 :(得分:0)

A - 解释

计时器只需要启动/停止机制和持续时间值的存储。另外,我们需要一个布尔标志(isCounting)来跟踪计时器的状态。

我在下面写了一个简单的演示代码;

B - 定时器类代码

package com.levent.timer;

public class Timer {

    private long timer;
    private long temp;
    private boolean isCounting;

    public Timer() {
        this.timer = 0;
        this.temp = 0;
        this.isCounting = false;
    }

    public void startTimer() {
        if(!isCounting) {
            isCounting = true;

            this.temp = System.currentTimeMillis();
        }
    }

    public void holdTimer() {
        if(isCounting) {
            isCounting = false;
            long delta = System.currentTimeMillis() - this.temp;

            this.timer += delta;
            this.temp = 0;
        }
    }

    public long getDuration() {
        return this.timer;
    }

    public void clearTimer() {
        this.timer = 0;
        this.temp = 0;
        this.isCounting = false;
    }
}

C - 计时器演示代码

package com.levent.timer;

public class TimerDemo {

    public static void main(String[] args) throws InterruptedException {
        Timer timer = new Timer();

        timer.startTimer();
        System.out.println("Timer Started");
        System.out.println("*************");
        System.out.println("timer initial value: " + timer.getDuration());

        /*
         * Wait timer for 1 sec, time will count this duration
         */
        Thread.sleep(1000);

        timer.holdTimer();
        System.out.println("timer value at T1  : " + timer.getDuration());

        /*
         * Wait timer for 2 sec, timer will NOT count on this duration
         * timer duration value won't change
         */
        Thread.sleep(2000);

        System.out.println("timer value at T2  : " + timer.getDuration());

        timer.startTimer();
        /*
         * Wait timer for 2 sec
         * Timer will count on this duration,
         * Expected timer duration value is ~3000 ms
         */
        Thread.sleep(2000);

        timer.holdTimer();
        System.out.println("timer value at T3  : " + timer.getDuration());
    }

}

D - 样本输出

Timer Started
*************
timer initial value: 0
timer value at T1  : 1000
timer value at T2  : 1000
timer value at T3  : 3001

答案 1 :(得分:0)

使用布尔变量来控制计时器

{
"msg": "Record added/updated",
"url": "index.php?option=com_content&view=article&id=24",
"baseRedirect": false,
"rowid": "0",
"redirect_how": "newpage",
"width": 300,
"height": 300,
"x_offset": 0,
"y_offset": 0,
"title": "",
"reset_form": true
}

启动计时器

public class SceneToScene extends AppCompatActivity {

boolean continueTimer = true;

制作方法Timer()

    Timer();

停止计时器,

private void Timer() {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something after 1000ms

            if (continueTimer) Timer();

        }
    }, 1000);
}

计时器将每1000毫秒(1秒)重复一次

重启,

continueTimer = false;