安卓:什么是Activity.runOnUiThread和View.post之间的区别?区别、runOnUiThread、Activity、post

2023-09-12 11:14:04 作者:两三星火

什么的区别 Activity.runOnUiThread View.post ,可能有人,请解释一下?

What's the difference between Activity.runOnUiThread and View.post, could someone, please, explain?

推荐答案

有没有真正的区别,不同之处在于 View.post 是有用的,当你没有直接访问活动。

There is no real difference, except that the View.post is helpful when you don't have a direct access to the activity.

在这两种情况下,如果没有对UI线程,Handler#post(Runnable)将被称为幕后。

In both cases, if not on UI thread, Handler#post(Runnable) will be called behind the scenes.

由于CommonsWare在注释中提到的,这两者之间的差异 - 对UI线程调用时,活动#runOnUiThread 将调用运行方法直接,而查看#交将发布可运行队列(如呼叫在处理程序#交

As CommonsWare mentioned in the comment, there is a difference between the two - when called on Ui thread, Activity#runOnUiThread will call the run method directly, while View#post will post the runnable on the queue (e.g. call the Handler#post)

最重要的一点IMO是,都具有相同的目标,而对于谁使用它,不应该有差别(和实现可能在未来改变)。

The important point IMO is that both have the same goal, and for whoever use it, there should be no difference (and the implementation may change in the future).