I have a requirement, where I need to create a timer task which will execute the function after every 10 sec. There is reset Button
, on click of that reset Button
I want to reset my time from 10 sec to 30 sec. Now after 30 sec when it execute the function I need to reset my timer again to 10 sec. I tried using Handler
, TimerTask
and CountDownTimer
, but not able to achieve the requirement. Can anyone suggest me the best way of solving this problem
// OnCreate of Activity
if (timerInstance == null) {
timerInstance = Timer()
timerInstance?.schedule(createTimerTask(), 10000L, 10000L)
}
private fun createTimerTask(): TimerTask {
return object : TimerTask() {
override fun run() {
Log.d("TimerTask", "Executed")
//presenter?.onCountdownTimerFinished(adapter.activeCallList, adapter.previousPosition)
}
}
}
//On Reset Button Click
timerInstance?.cancel()
timerInstance = Timer()
timerInstance?.schedule(createTimerTask(), 30000L, 30000L)