什么是Android UiThread(UI线程) [英] What is the Android UiThread (UI thread)

查看:418
本文介绍了什么是Android UiThread(UI线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我解释一下究竟是什么在UI线程? 在developer.android.com它说,有关runOnUiThread功能

Can someone explain to me what exactly the UI thread is? On developer.android.com it says about the runOnUiThread function

公众最终无效runOnUiThread(Runnable的动作)

public final void runOnUiThread (Runnable action)

自:API级别1运行在UI线程上指定的操作。如果   当前线程是UI线程,则该动作被执行   立即。如果当前线程不是UI线程,动作   贴到UI线程的事件队列。

Since: API Level 1 Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

请问UI线程意味着,这将通过像来电或屏幕变暗等一些UI活动运行每次活动被推向了后台?如果不是,究竟是什么在UI线程包括哪些内容?

Does the UI thread mean that this will be run everytime the activity is pushed the the background by some ui activity like incoming call or screen dimming etc.? If not, what exactly does the UI thread include ?

感谢您

推荐答案

该UIThread是执行的应用程序的主线程。这是大多数的应用程序code运行。你所有的应用程序组件(活动,服务,ContentProviders,BroadcastReceivers)都在此线程创建的,任何系统调用这些组件都在这个线程执行。对

The UIThread is the main thread of execution for your application. This is where most of your application code is run. All of your application components (Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those components are performed in this thread.

例如,假设你的应用程序是一个Activity类。然后,所有的生命周期方法和大部分的事件处理code的运行在这个UIThread。这些都是像的onCreate 的onPause 的onDestroy ,<方法code>的onClick 等。此外,这是所有的更新到用户界面制成。任何导致用户界面进行更新或更改已发生在UI线程上。

For instance, let's say your application is a single Activity class. Then all of the lifecycle methods and most of your event handling code is run in this UIThread. These are methods like onCreate, onPause, onDestroy, onClick, etc. Additionally, this is where all of the updates to the UI are made. Anything that causes the UI to be updated or changed HAS to happen on the UI thread.

有关应用程序的进程和线程的详细信息请点击这里。

当你明确地产生一个新的线程来完成工作的背景,code未在UIThread运行。因此,如果此背景线程需要做一些事情,改变了用户界面发生了什么?这是 runOnUiThread 是什么。其实你应该使用一个处理程序(请参见下面的链接以获取更多信息)。它提供了这些后台线程执行code,可以修改UI的能力。他们通过把用户界面,修改code在一个Runnable对象,并将其传递给runOnUiThread方法。

When you explicitly spawn a new thread to do work in the background, this code is not run on the UIThread. So what happens if this background thread needs to do something that changes the UI? This is what the runOnUiThread is for. Actually you're supposed to use a Handler (see the link below for more info on this). It provides these background threads the ability to execute code that can modify the UI. They do this by putting the UI-modifying code in a Runnable object and passing it to the runOnUiThread method.

有关产卵工作线程和更新来自它们的用户界面点击这里

我个人只用在我的测试仪器在 runOnUiThread 方法。由于测试code不会在UIThread执行,则需要使用此方法来运行code表示修改UI。所以,我用它来点击和关键事件,注入我的应用程序。然后,我可以检查应用程序的状态,以确保正确的事情发生了。

I personally only use the runOnUiThread method in my Instrumentation Tests. Since the test code does not execute in the UIThread, you need to use this method to run code that modifies the UI. So, I use it to inject click and key events into my application. I can then check the state of the application to make sure the correct things happened.

有关测试的UIThread更多的信息和运行code。单击此处

这篇关于什么是Android UiThread(UI线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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