Since its beginning, Android has provided a Handler
API. As the documentation states, it allows you to deliver messages from a queue on a Looper
’s thread.
从一开始,Android就提供了Handler
API。 如文档所述,它允许您从Looper
线程上的队列传递消息。
- Handler().postDelayed({
- doSomething()}, delay)
This API is handy and yet so sneaky. Don’t let yourself be fooled when using it on a view. To understand where the danger lies, we need to dig deeper into the View
class.
这个API很方便,但是却很偷偷摸摸。 在视图上使用它时,不要让自己上当。 要了解危险所在,我们需要更深入地研究View
类。
视图处理程序 (Handlers for Views)
View
in Android leverages the Handler
API. It acts as a passthrough with an inner Handler
and exposes both post()
and postDelayed()
functions.
Android中的View
利用Handler
API。 它充当内部Handler
postDelayed()
并公开post()
和postDelayed()
函数。
Have a closer look at its implementation (at the time I’m writing the article):
仔细研究其实现(在撰写本文时):
- /**
- * <p>Causes the Runnable to be added to the message queue, to be run
- * after the specified amount of time elapses.
- * The runnable will be run on the user interface thread.</p>
- *
- * @param action The Runnable that will be executed.
- *