如何在Kotlin中恢复暂停的CountDownTimer

时间:2019-11-08 18:27:07

标签: android kotlin countdowntimer

这个想法是制造一个国际象棋时钟。但是问题是恢复了CountDownTimer的实例暂停,而没有重置或创建新实例。

第一个动作是触摸“白色按钮”,然后调用方法“ startTimerBlack”:黑色计时器开始运行,白色计时器暂停。

触摸“黑色按钮”时,将调用“ startTimerWhite”:白色计时器开始运行,黑色计时器暂停。

再次触摸白色按钮时,黑色计时器应重新启动,但不是。

class MainActivity : AppCompatActivity() {
    enum class TimerState {
        Stopped, Paused, Running
    }

    private lateinit var timerWhite: CountDownTimer
    private lateinit var timerBlack: CountDownTimer
    private var timerStateWhite = TimerState.Stopped
    private var timerStateBlack = TimerState.Stopped
    private var secondsRemaining = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        buttonWhite.setOnClickListener { v ->
            println("white")
            startTimerBlack()
        }
        buttonBlack.setOnClickListener { v ->
            println("black")
            startTimerWhite()
        }
    }

    private fun startTimerWhite() {
        if(timerStateBlack != TimerState.Stopped) {
            timerBlack.cancel()
            timerStateBlack = TimerState.Paused
        }

        timerStateWhite = TimerState.Running
        timerWhite = object : CountDownTimer(1000 * 1000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                secondsRemaining = millisUntilFinished / 1000
                buttonWhite.text = secondsRemaining.toString()
            }
            override fun onFinish() {}
        }.start()
    }

    private fun startTimerBlack() {
    if(timerStateWhite != TimerState.Stopped) {
        timerWhite.cancel()
        timerStateWhite = TimerState.Paused
    }

    timerStateBlack = TimerState.Running
    timerBlack = object : CountDownTimer(secondsRemaining, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            secondsRemaining = millisUntilFinished / 1000
            buttonBlack.text = secondsRemaining.toString()
        }

        override fun onFinish() {

        }
    }.start()
  }
}

2 个答案:

答案 0 :(得分:0)

timerWhite = object : CountDownTimer(1000 * 1000, 1000)

如果我是正确的话,则一旦timerWhite开始其第一次计时,以上内容就会将secondsRemaining重置为999000 / 1000 = 999。结果,whiteTimer总是在值1000s处重新启动,而blackTimer总是在whiteTimer停止时剩下的时间开始。

但是,如果我正确理解了国际象棋时钟,则应该为每个计时器分别保存时间。如下所示:

private var whiteSecondsRemaining = 1000
private var blackSecondsRemaining = 1000
...
timerWhite = object : CountDownTimer(whiteSecondsRemaining , 1000) {
    // update whiteSecondsRemaining in onTick()
}
...
timerBlack = object : CountDownTimer(blackSecondsRemaining , 1000) {
    // update blackSecondsRemaining in onTick()
}

答案 1 :(得分:0)

我认为这可以在两个Thread和时间内完成。由于处理方式不同。签出这个非常简单的代码即可。

class MainActivity : AppCompatActivity() {
    private val DELAY = TimeUnit.SECONDS.toMillis(3)
    private val chessTimer = ChessTimer("Sample")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        startTask()
        chessTimer.start()
    }

    private fun startTask() {
        Handler(Looper.getMainLooper()).postDelayed({
            chessTimer.changeTime()
            startTask()
        }, DELAY)
    }

    private class ChessTimer(name: String) : Thread(name) {
        var time1 = TimeUnit.MINUTES.toMillis(3)
        var time2 = TimeUnit.MINUTES.toMillis(3)

        var active: Boolean = true
        var startTime : Long = 0

        override fun run() {
            var whichAction = active

            while (true) {
                synchronized(this) {
                    if (active != whichAction) {
                        /**
                         * Action was changed!
                         */
                        val delta = System.currentTimeMillis() - startTime
                        Log.d("Sample", "Current time: ${System.currentTimeMillis()} Delta $delta")
                        if (whichAction) time1 -= delta
                                else time2 -= delta

                        whichAction = active
                        startTime = System.currentTimeMillis()
                        Log.d(
                            "Sample", "Time one $time1." +
                                    " Time second $time2"
                        )
                    }

                }
            }
        }

        override fun start() {
            startTime = System.currentTimeMillis()
            active = true
            super.start()
        }

        fun changeTime() {
            synchronized(this) {
                Log.d("Sample", "Time: ${System.currentTimeMillis()} ")
                active = !active
            }
        }
    }
}