Shanmuka Chowdary
Shanmuka Chowdary

Reputation: 98

CountDown Timer OnTick method Not Working when I give Time

I'm Running one count down timer for 15 minutes which is equals to 100%,

When I'm in the app it's fine working for me. What i'm doing is when i'm closing the app I'm saving start time and opening the app taking current time and calculating the remaining time. I'm giving the remaining time to ontick method before starting the countdown timer. on the first tick it's showing correct but for the next tick,It's starting from 15 minutes. Can anyone tell why it's happenning

 countDownTimer = object : CountDownTimer(900000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    activity?.runOnUiThread {
                        val minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)
                        val seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))

                        val progress = 100 - ((((minutes / 15f) + (seconds / 900f)) * 100).toInt())

                        if (progress == 100) {
                            countDownTimer.cancel()
                            batteryHealthView.hideProgress()
                        }
                        saveData(progress, percentageString)
                    }
                }

                override fun onFinish() {
                    activity?.runOnUiThread {
                        countDownTimer.cancel()
                    }
                }
            }
            if (timeLeft!! > 0) {
                countDownTimer.onTick(timeLeft)
            }
            countDownTimer.start()

Upvotes: 1

Views: 701

Answers (1)

user4571931
user4571931

Reputation:

Share code how you are calling CountDownTimer(900000, 1000) Its seems you are using 900000 each time, Instead you need to re calculate remain time and pass that time in method call.

Upvotes: 2

Related Questions