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

相关文章
相关标签/搜索