Android采用UI单线程模型,所以工作线程(非UI线程)与UI线程的通信是不可避免的。工作线程与UI主线程通信(进行更新UI等操作)主要有以下三种方式。
First :
Looper.getMainLooper()
new Handler(Looper.getMainLooper()).post(task);
Second :
Activity#runOnUiThread()
runOnUiThread(task);
The implements of runOnUiThread is shown as bellow :
Third:
There is also a third way to execute a Runnable on the UI thread which would be View#post(Runnable) - this one will always post the message even when called from the UI thread. That is useful since that will ensure that the View has been properly constructed and has a layout before the code is executed.
原文:http://blog.csdn.net/handsome3/article/details/22807125