Android中不能在子线程中更新View视图的原因
这是一条规律,很多coder知道,但原因是什么呢?
如下:
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.
SDK API中Handler章节中的一段。
意思是:
当进程的主线程创建后,将启动一个MessageQueue,用来管理Top-level组件对象(activity,broadcastReceiver)和你创建的所有窗口[也就是包括更新View的操作],你可以创建子线程,并在子线程中用Handler来与main线程通信。通过在子线程中调用Handler的post或sendMessage方法来实现。你所传递的Runnable或Message将在主线程Handler的MessageQueue中参与调度,并且在适当的时候执行。
最近在深入研究Handler,不是那么简单,累。
如果说错了,欢迎拍砖!