Handler与runOnUiThread更新UI有什么区别?
分别更适合在哪种情况下用?
阿神2017-04-17 17:58:13
In terms of implementation principles, there is no difference between the two. runOnUiThread
也是借助Handler
实现的。
对于使用场景,runOnUiThread
用法简单,并且共享了同一个Handler
,用起来高效、方便。另外,如果在主线程中直接调用,runOnUiThread
也可以判断并立即执行,不再推入消息队列。
而Handler
由于更加基础,所以可定制性要比runOnUiThread
Stronger, it can implement functions such as marking and delay, and can be pushed into other message loop threads instead of the main thread.
PHP中文网2017-04-17 17:58:13
runOnUiThread directly encapsulates a Runnable object into a Message and hands it to the Looper of the main thread for execution. The execution code is:
handler.post(mRunnable);
If Handler wants to get the same effect, first the Looper bound by Handler must be the Looper of the main thread, which can be obtained through Looper.getMainLooper(). Then also send a runable object through post.
There is essentially no difference.