Android - loop part of the code every 5 seconds

后端 未结 4 2010
醉酒成梦
醉酒成梦 2020-11-30 06:51

I would like to start repeating two lines of code every 5 seconds when I press the button START and end it, when I press the button STOP. I was trynig with a TimerTask and H

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 07:29

    I don't have much more to add, other than to mention the differences between using Handler, CountDownTimer, and regular Timer. As britzl mentioned, the CountDownTimer uses a Handler internally, so that is equivalent to using the handler directly. A handler is used for running Ui stuff, for very short periods of time. An example would be setText for a text view. For computationally intensive tasks, handlers may cause a lag. A timer also can only run short tasks, but it is not necessarily only for UI stuff. For more complicated tasks, a new Thread should be used.

提交回复
热议问题