为什么倒数计时器在Android中使用处理?计时器、倒数、Android

2023-09-06 09:37:20 作者:轮回千年一场梦

的倒计时器表明,它采用了处理程序。有没有使用任何处理具体的原因?因为当我们正在做使用线程的一些用户交互处理程序一般都采用。但这里有没有线程,我可以在倒数计时器看到的。而且在UI线程它自身使用时,倒数计时器工作。

解决方案   

由于我们正在做使用线程的一些用户交互处理程序一般都是用

真。然而,一般!=总是。

恰巧处理程序有计时的目的,如 postDelayed()和 sendMessageDelayed(),其中 CountDownTimer 需要的优势。您可以使用那些自己为好。他们是很好的和轻量级的,因为,当你注意,我们并不需要一个单独的线程,事物的方式像定时的TimerTask 做的。

  VB中用Timer做一个倒数计时器的问题

和也是在UI线程它自身使用时,倒数计时器工作。

它需要一个线程带有附加尺蠖使用处理程序 CountDownTimer 可能是一般的主应用程序线程的使用,如果不说,在一些单独的 HandlerThread

The GrepCode of count down timer shows that it uses a Handler. Is there any specific reason for using handlers? Because handlers are generally used when we are doing some user interaction using threads. But here there are no threads that I can see in Countdown Timer. And also Countdown Timer works when used in the UI thread it self.

解决方案

Because handlers are generally used when we are doing some user interaction using threads

True. However, "generally" != "always".

It so happens that Handler has useful methods for timing purposes, like postDelayed() and sendMessageDelayed(), which CountDownTimer takes advantage of. You can use those yourself as well. They are nice and lightweight because, as you note, we do not need a separate thread, the way things like Timer and TimerTask do.

And also Countdown Timer works when used in the UI thread it self.

It needs a thread with an attached Looper to use Handler. CountDownTimer is probably usually used on the main application thread, and if not that, on some separate HandlerThread.