How to clear CountDownTimer from onTick method?

前端 未结 3 1649
一个人的身影
一个人的身影 2021-01-23 06:04

I have implemented the timer functionality in android app, I able to finish the timer from other local methods by calling timerObject.cancel() . Its working perfect

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 06:33

    Looking at the source code for CountDownTimer it is easy to see why it is not working. cancel() merely removes the message for ticking the timer from the queue. But the message handler that gets called at each tick posts a message for the next tick after calling onTick(). So either you have to call cancel() outside of onTick(), via a Handler for example, or switch to using the Timer class instead.

提交回复
热议问题