Handler 是什么?
這個問題其實在 Handler 源碼最前面的注釋里其實已經介紹的很清楚了,下面都是我自己的翻譯,英語好的可以自己理解:
- 我們先來看第一段話:
/**
* A Handler allows you to send and process {@link Message} and Runnable
* objects associated with a thread's {@link MessageQueue}. Each Handler
* instance is associated with a single thread and that thread's message
* queue. When you create a new Handler, it is bound to the thread /
* message queue of the thread that is creating it -- from that point on,
* it will deliver messages and runnables to that message queue and execute
* them as they come out of the message queue.
這句話介紹了什么是 Hanlder 大概意思是說(這種格式看起來比較容易):
* 一個 Hanlder 允許你去發送和處理與 Thread(線程) 的 MessageQueue(消息隊列) 相關聯的
* Message(消息) 和 Runnable 對象。每個 Hanlder 實例關聯一個 Thread 實例和該 Thread 的
* MessageQueue。當你創建一個新的 Hanlder 時,它(Handler)會和當前 Thread/當前 Thread 的
* MessageQueue 聯系起來 -- 此時,它(Handler)會將 Messages 和 Runnables 傳遞到該 MessageQueue
* 并在他們從消息隊列中出現時執行它們。
- 第二段話:
* There are two main uses for a Handler: (1) to schedule messages and
* runnables to be executed as some point in the future; and (2) to enqueue
* an action to be performed on a different thread than your own.
這句話介紹了 Handler 的兩個主要用途:
- 傳入 Message 和 Runnable 用來執行。
- 在另一個線程上執行一個操作。
- 第三段話:
* cheduling messages is accomplished with the
* {@link #post}, {@link #postAtTime(Runnable, long)},
* {@link #postDelayed}, {@link #sendEmptyMessage},
* {@link #sendMessage}, {@link #sendMessageAtTime}, and
* {@link #sendMessageDelayed} methods. The <em>post</em> versions allow
* you to enqueue Runnable objects to be called by the message queue when
* they are received; the <em>sendMessage</em> versions allow you to enqueue
* a {@link Message} object containing a bundle of data that will be
* processed by the Handler's {@link #handleMessage} method (requiring that
* you implement a subclass of Handler).
這句話首先介紹了 Handler 調度 Message 的幾種方法:
- post
- postAtTime(Runnable,long)
- postDelayed
- sendEmptyMessage
- sendMessage
- sendMessageAtTime
- sendMessageDelayed
post 允許你在接收到 MessageQueue 時調用 Runnable 對象。
sendMessage 允許你對包含一些數據的 Message 對象進行排列,這些數據將由 handlerMessage 方法處理。
- 第四段話:
* <p>When posting or sending to a Handler, you can either
* allow the item to be processed as soon as the message queue is ready
* to do so, or specify a delay before it gets processed or absolute time for
* it to be processed. The latter two allow you to implement timeouts,
* ticks, and other timing-based behavior.
大概意思是:
當發布或者發送到 Handler 時,只要 MessageQueue 準備就緒就可以立即處理該任務。或者在處理 MessageQueue 之前指定一個延遲或者一個固定的時間。后兩者允許你實現延時和一些其他的時間設置。
- 最后一段:
* When a
* process is created for your application, its main thread is dedicated to
* running a message queue that takes care of managing the top-level
* application objects (activities, broadcast receivers, etc) and any windows
* they create. You can create your own threads, and communicate back with
* the main application thread through a Handler. This is done by calling
* the same <em>post</em> or <em>sendMessage</em> methods as before, but from
* your new thread. The given Runnable or Message will then be scheduled
* in the Handler's message queue and processed when appropriate.
當一個進程在應用中被創建之后,它的主線程致力于運行一個負責管理頂層應用對象(Activity,Broadcast Receiver,etc)以及它創建一些 Window 的 MesssageQueue。你可以創建一些你自己的線程(子線程)來通過 Handler 和主線程通信。和以前一樣,它需要調用 post 和 sendMessage 方法來完成,但是要在你新創建的子線程中。傳遞過來的 Message 或者 Runnable 將會在 Handler 的 MessageQueue 中調度,并在合適的時候處理。