Android: How to pause and resume a Count Down Timer?

后端 未结 8 1633
长发绾君心
长发绾君心 2020-12-02 17:20

I have developed a Count Down Timer and I am not sure how to pause and resume the timer as the textview for the timer is being clicked. Click to start then click again to pa

8条回答
  •  猫巷女王i
    2020-12-02 17:48

    I wrote a extended CountDownTimer here. Let's try this and leave your comment:

    Eg:

    // Init timer
    lateinit var timerExt: CountDownTimerExt
    
    timerExt = object : CountDownTimerExt(TIMER_DURATION, TIMER_INTERVAL) {
        override fun onTimerTick(millisUntilFinished: Long) {
            Log.d("MainActivity", "onTimerTick $millisUntilFinished")
        }
    
        override fun onTimerFinish() {
            Log.d("MainActivity", "onTimerFinish")
        }
    
    }
    
    // Start/Resume timer
    timerExt.start()
    
    // Pause timer
    timerExt.pause()
    
    // Restart timer
    timerExt.restart()
    

提交回复
热议问题