什么是Activity.runOnUiThread(可运行操作)和Handler.post()之间的区别是什么?

编程入门 行业动态 更新时间:2024-10-10 09:17:49
本文介绍了什么是Activity.runOnUiThread(可运行操作)和Handler.post()之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

什么是使用之间的区别/优点/缺点 Activity.runOnUiThread 或 Handler.post(可运行动作)的的android?

What's the differences/ advantages / drawbacks between using Activity.runOnUiThread or Handler.post(runnable action) in android ?

推荐答案

Activity.runOnUiThread,就像它的名字所暗示的,将执行的可运行在目前负责的UI线程。所以,如果你有一个CPU密集型任务,它可以使用户界面无响应的时间很短。相反,处理程序提供了一种方法可以让你创建一个线程,运行一些code,并通知用户界面,当你完成(即Handler.sendMessage)。

Activity.runOnUiThread, like it's name implies, will execute the Runnable in the thread that is currently responsible for the UI. So, if you have a CPU intensive task, it can make the UI unresponsive for a short period of time. Conversely, Handler provides a way for you to create a thread, run some code, and notify the UI when you are done (i.e Handler.sendMessage).

该文档的处理程序状态这比我更可以:

The docs for Handler state this better than I can:

当为创建一个过程你   应用程序,它的主线程   专门用来运行一个消息队列   这需要管理的护理   顶级应用程序对象   (活动,广播接收机等)   和任何窗口他们创造。您可以   创建自己的线程,   与主通信回   通过处理程序应用程序线程。   这是通过调用同一职位做   或方法的sendMessage像以前一样,但   从你的新的线程。给定   Runnable或信息会比被   定处理程序的消息   排队并处理在适当的时候。

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.

更多推荐

什么是Activity.runOnUiThread(可运行操作)和Handler.post()之间的区别是什么?

本文发布于:2023-11-27 09:45:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   操作   runOnUiThread   Activity   Handler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!