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.
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.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:
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.