android - Handler与runOnUiThread更新UI的区别
PHP中文网
PHP中文网 2017-04-17 17:56:13
0
2
609

Handler与runOnUiThread更新UI有什么区别?
分别更适合在哪种情况下用?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
阿神

In terms of implementation principles, there is no difference between the two. runOnUiThread也是借助Handler实现的。
对于使用场景,runOnUiThread用法简单,并且共享了同一个Handler,用起来高效、方便。另外,如果在主线程中直接调用,runOnUiThread也可以判断并立即执行,不再推入消息队列。
Handler由于更加基础,所以可定制性要比runOnUiThreadStronger, it can implement functions such as marking and delay, and can be pushed into other message loop threads instead of the main thread.

洪涛

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template