Countdown timer starts from a second after it is supposed to

后端 未结 2 1624
陌清茗
陌清茗 2020-12-22 11:30

I have a Countdowntimer object. Basically the timer is supposed to countdown from 10 to 1. What it does is starts the timer from 9 and it ends on 1 but it stays on 1 for abo

2条回答
  •  鱼传尺愫
    2020-12-22 11:42

    The reason is because the time interval isn't fine enough and callback isn't at exactly quantas of 10,000

    This code in Kotlin is presenting the remaining time punctuately:

    private fun startTimer()
    {
        Log.d(TAG, ":startTimer: timeString = '$timeString'")
    
        object : CountDownTimer(TASK_SWITCH_TIMER, 250)
        {
            override fun onTick(millisUntilFinished: Long)
            {
                val secondsUntilFinished : Long = 
                Math.ceil(millisUntilFinished.toDouble()/1000).toLong()
                val timeString = "${TimeUnit.SECONDS.toMinutes(secondsUntilFinished)}:" +
                        "%02d".format(TimeUnit.SECONDS.toSeconds(secondsUntilFinished))
                Log.d(TAG, ":startTimer::CountDownTimer:millisUntilFinished = $ttlseconds")
                Log.d(TAG, ":startTimer::CountDownTimer:millisUntilFinished = $millisUntilFinished")
            }
    
            @SuppressLint("SetTextI18n")
            override fun onFinish()
            {
                timerTxtVw.text = "0:00"
                gameStartEndVisibility(true)
            }
        }.start()
    }
    

提交回复
热议问题