[杂谈]关于Handler.post与View.post的区别

Android的线程分UI线程与非UI线程两类。而Handler是非UI线程向UI线程传递消息的桥梁。

除了非常常用sendMessage之外,Handler也提供了post(Runnable...),postDelayed等

风骚犀利的方法,利于我们使用“闭包式编程”。

 

View作为UI的一部分,也无例外的提供了一个淫荡的View.post(Runnable)方法,让我们在

UI线程中doSomeThing。灵活使用View.post。神马Handler,Activity.runOnUIThread,

都可以一边站去。

 

相同

在与UI线程的通信上,Handler与View,其实最终都做了同样的事情。就是将消息传递在UI线程

的消息队列里,执行一些处理操作。

 

不同的是

View.post方法想在非UI线程有效工作。如该方法的注释所说,必须保证该View已经被添加至窗口。

This method can be invoked from outside of the UI thread only when this View is attached to a window.

 另外给一个stackoverflow的例子:

 http://stackoverflow.com/questions/4083787/runnable-is-posted-successfully-but-not-run

 

posted @ 2013-01-26 13:26  Zenip  阅读(2707)  评论(0编辑  收藏  举报