Home  >  Q&A  >  body text

android - Handler与runOnUiThread更新UI的区别

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

PHP中文网PHP中文网2741 days ago562

reply all(2)I'll reply

  • 阿神

    阿神2017-04-17 17:58:13

    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.

    reply
    0
  • PHP中文网

    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.

    reply
    0
  • Cancelreply