什么是Android UiThread(UI线程)

发布于 2024-09-18 07:21:53 字数 340 浏览 8 评论 0原文

有人可以向我解释一下 UI 线程到底是什么吗? 在developer.android.com上它提到了runOnUiThread函数

public final void runOnUiThread(可运行操作)

自:API 级别 1 在 UI 线程上运行指定的操作。如果 当前线程是UI线程,然后执行action 立即地。如果当前线程不是 UI 线程,则操作为 发布到 UI 线程的事件队列。

UI 线程是否意味着每次活动被某些 ui 活动(如来电或屏幕变暗等)推到后台时都会运行?如果不是,UI 线程到底包含什么?

谢谢

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

public final void runOnUiThread (Runnable action)

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.

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 ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2024-09-25 07:21:53

UIThread 是应用程序的主执行线程。这是大部分应用程序代码运行的地方。所有应用程序组件(Activities、Services、ContentProviders、BroadcastReceivers)都是在此线程中创建的,并且对这些组件的任何系统调用都在此线程中执行。

例如,假设您的应用程序是单个 Activity 类。然后,所有生命周期方法和大部分事件处理代码都在此 UIThread 中运行。这些方法包括 onCreateonPauseonDestroyonClick 等。此外,这是所有对 UI 进行了更新。任何导致 UI 更新或更改的事情都必须在 UI 线程上发生。

有关应用程序进程和线程的更多信息,请单击此处。

您显式地生成一个新线程在后台工作,此代码不在 UIThread 上运行。那么如果这个后台线程需要做一些改变 UI 的事情会发生什么呢?这就是 runOnUiThread 的用途。实际上,您应该使用处理程序(有关更多信息,请参阅下面的链接)。它为这些后台线程提供了执行可修改 UI 的代码的能力。他们通过将 UI 修改代码放入 Runnable 对象并将其传递给 runOnUiThread 方法来实现此目的。

有关生成工作线程并从中更新 UI 的更多信息,请单击此处

我个人只在仪器测试中使用 runOnUiThread 方法。由于测试代码不在UIThread中执行,因此您需要使用此方法来运行修改UI的代码。因此,我使用它将单击和按键事件注入到我的应用程序中。然后我可以检查应用程序的状态以确保发生了正确的事情。

有关在 UIThread 上测试和运行代码的更多信息,请单击此处

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.

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.

For more info on your application's Processes and Threads click here.

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.

For more info on spawning worker threads and updating the UI from them click here

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.

For more info on testing and running code on the UIThread click here

空城仅有旧梦在 2024-09-25 07:21:53

如果您在单独的线程中执行阻塞代码(例如 Http-Request),请考虑使用 AsyncTask。它的 doInBackground 方法在单独的线程上运行。 AsyncTask 为您提供了onProgressUpdateonPostExecute 方法,它们保证在UI 线程上运行

如果您需要 GUI 进度更新(例如通过进度条),请在 doInBackground 内调用 publishProgress。这会导致随后调用 onPublishProgress,它也保证在 UI 线程上运行

onPostExecutedoInBackground 返回后自动调用。

If you execute blocking code (e.g. a Http-Request) in a separate Thread, consider using AsyncTask. Its doInBackground-Method runs on a separate Thread. AsyncTask provides you with methods onProgressUpdate and onPostExecute which are guaranteed to run on the UI thread.

If you need GUI-progress updates (e.g. via a progressbar) call publishProgress inside doInBackground. This leads to a subsequent call of onPublishProgress which is also guaranteed to run on the UI thread.

onPostExecute is automatically called after doInBackground returns.

鹿港小镇 2024-09-25 07:21:53

所有 UI 绘图等都发生在单独的线程中。它称为 UIThread。如果你想对 UI 进行任何更改,你必须确保它发生在 UIThread 的上下文中。
最简单的方法是使用 runOnUiThread

All UI drawings etc. happen in a separate thread. Its called the UIThread. If you want to make any change to UI u must use make sure it happens in UIThread's context.
Easiest way of doing it is to make use of runOnUiThread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文