Android中不能在子線程中更新View視圖的緣由

這是一條規律,不少coder知道,但緣由是什麼呢?web

以下:windows

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.app

SDK API中Handler章節中的一段。post

意思是:spa

當進程的主線程建立後,將啓動一個MessageQueue,用來管理Top-level組件對象(activity,broadcastReceiver)和你建立的全部窗口[也就是包括更新View的操做],你能夠建立子線程,並在子線程中用Handler來與main線程通訊。經過在子線程中調用Handler的post或sendMessage方法來實現。你所傳遞的Runnable或Message將在主線程Handler的MessageQueue中參與調度,而且在適當的時候執行。線程

 

最近在深刻研究Handler,不是那麼簡單,累。code

若是說錯了,歡迎拍磚!orm

相關文章
相關標籤/搜索