什么是Activity.runOnUiThread(可运行操作)和Handler.post()之间的区别是什么? [英] What's the difference between Activity.runOnUiThread(runnable action) and Handler.post()?

查看:265
本文介绍了什么是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()之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆