为什么倒数计时器在Android中使用处理? [英] Why does CountDown Timer in Android use a Handler?

查看:188
本文介绍了为什么倒数计时器在Android中使用处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的<一个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, 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.

这篇关于为什么倒数计时器在Android中使用处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆