gpt4 book ai didi

android - Activity.runOnUiThread(runnable action) 和 Handler.post() 有什么区别?

转载 作者:IT老高 更新时间:2023-10-28 21:52:53 31 4
gpt4 key购买 nike

在 android 中使用 Activity.runOnUiThreadHandler.post(runnable action) 有什么区别/优点/缺点?

最佳答案

Activity.runOnUiThread,顾名思义,将在当前负责 UI 的线程中执行 Runnable。因此,如果您有一个 CPU 密集型任务,它可能会使 UI 在短时间内无响应。相反,Handler 为您提供了一种创建线程、运行一些代码并在完成后通知 UI 的方法(即 Handler.sendMessage)。

Handler 的文档比我能更好地说明这一点:

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will than be scheduled in the Handler's message queue and processed when appropriate.

关于android - Activity.runOnUiThread(runnable action) 和 Handler.post() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1839625/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com