为什么倒数计时器在Android中使用处理? [英] Why does CountDown Timer in Android use a Handler?
问题描述
的<一个grep的code href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.4_r1/android/os/CountDownTimer.java\"相对=nofollow>倒计时器表明,它采用了处理程序。有没有使用任何处理具体的原因?因为当我们正在做使用线程的一些用户交互处理程序一般都采用。但这里有没有线程,我可以在倒数计时器看到的。而且在UI线程它自身使用时,倒数计时器工作。
由于我们正在做使用线程的一些用户交互处理程序一般都是用
块引用>真。然而,一般!=总是。
恰巧
处理程序
有计时的目的,如postDelayed()
和<$ C $有用的方法C> sendMessageDelayed(),其中CountDownTimer
需要的优势。您可以使用那些自己为好。他们是很好的和轻量级的,因为,当你注意,我们并不需要一个单独的线程,事物的方式像定时
和的TimerTask
做的。
和也是在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, likepostDelayed()
andsendMessageDelayed()
, whichCountDownTimer
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 likeTimer
andTimerTask
do.And also Countdown Timer works when used in the UI thread it self.
It needs a thread with an attached
Looper
to useHandler
.CountDownTimer
is probably usually used on the main application thread, and if not that, on some separateHandlerThread
.这篇关于为什么倒数计时器在Android中使用处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!