官方文檔 http://developer.android.com/reference/android/os/Handler.html 有進行解釋html
其實我自己很好奇asynctask是怎麼實現的。java
A Handler allows you to send and process
Message
and Runnable objects associated with a thread'sMessageQueue
. 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.androidThere 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.windows
Scheduling messages is accomplished with the
post(Runnable)
,postAtTime(Runnable, long)
,postDelayed(Runnable, long)
,sendEmptyMessage(int)
,sendMessage(Message)
,sendMessageAtTime(Message, long)
, andsendMessageDelayed(Message, long)
methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue aMessage
object containing a bundle of data that will be processed by the Handler'shandleMessage(Message)
method (requiring that you implement a subclass of Handler).appWhen 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.async
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 post or sendMessage 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.oop
Handler容許你經過MessageQueue發送消息或者是可運行的對象。一個handler只跟一個線程的消息隊列相對應。一旦你建立了handler它便與當前進程綁定,它可以傳遞或者是執行消息或者是可執行對象。post
用處有兩,一、執行未來的消息和可執行對象;二、或者是讓代碼在其餘的線程被執行。ui
可用方法有post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long)。POST能讓你執行可執行對象,而sendMessage則能夠發送相似bundle的數據。當你發送這些東西的時候還能決定要何時執行這些消息和對象。spa
關於handler的綁定,若是你想綁定本線程,那麼直接NEW就好了,可是若是你想把NEW出來的handler綁定到你指定的子線程中,那麼就必須在NEW的時候給一個參數LOOPER,例如:(代碼來自 http://www.itzhai.com/android-the-role-and-use-of-handler.html )
HandlerThread handlerThread = new HandlerThread("hanlderThread"); //必須先調用該類的start的方法,不然該獲取的Looper對象爲NULL handlerThread.start(); MyHanlder myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); Bundle bundle = new Bundle(); bundle.putString("name","arthinking"); msg.setData(b); msg.sendToTarget();
便可給指定的子線程發送message數據。固然得到了handler還能POST可執行的那些對象。
官方的AsyncTask就是經過handler來實現把結果交給UI線程處理的。