了解 UI 线程

Understanding UI thread

例如,有很多任务发布到UI线程如下。

Handler handler = new Handler(Looper.getMainLooper());

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Some logic to display/update something in UI
    }
}, 5000);

当 android 应用程序进入后台时,这些任务会发生什么情况?

这些任务会在后台处理吗?完整的UI线程会在后台挂起吗?要么?发布到 UI 线程的所有任务都已暂停?

UI 线程在 activity 完全加载后没有任务发布时会发生什么情况? Looper里没有任务会不会被挂起?

提前致谢。

UI 线程也称为主线程,它是您打开应用程序时默认启动的线程,除非您将它们卸载到其他线程,否则所有交互都将在此处发生。它不依赖于应用程序的可见性,只要应用程序存在,它就会 运行ning。没有被系统挂起。

What happens to UI thread when there are no tasks posted to it after the activity is completely loaded? Will it be suspended, if there are no tasks are in the Looper?

让我们从 Looper 概念开始。这对于了解 UI 线程的工作方式非常重要。 Looper 是一个在给定线程中循环并处理传入消息的东西,当没有任务时它正在循环并等待新任务,因此,回答你的最后一个问题,线程不会被挂起。 Looper 被设计成即使在没有任务的情况下也能保持线程存活。 UI Android 中的线程默认有自己的 Looper。

What happens to these tasks when android application gone to background? Will these tasks are processed even in the background? Will the complete UI thread be suspended in background? Or? All the tasks posted to UI thread are suspended?

进入后台不会改变主线程的任何内容。 运行ning 直到应用程序被终止。因此,您安排在处理程序上的代码将只是 运行。

旁注。这里可能的问题是,虽然 activity 在后台,但它可能会被系统杀死,而您尝试更改 UI 会使应用程序崩溃(有一些类似的情况),但它是与 UI 线程没有真正的关系。

总而言之,UI线程在任何情况下都不会被框架挂起。它只是 运行s 并处理你给它的所有东西,或者等到新任务给它。

Here你可以在官方文档中找到主线程的描述。

This 是一个关于 looper 的很好的 SO 线程。

让我们先了解几个术语。

Handler:

A Handler allows communicating back with UI thread from other background thread. This is useful in android as android doesn’t allow other threads to communicate directly with UI thread. In technical terms, it is a way to post Messages or runnable to your associated thread message queue.

所以处理程序总共执行了两项任务

  • 安排消息和可运行程序在未来某个时间点执行;和
  • 将要在不同于您自己的线程上执行的操作排队。

那么如何安排一个

post(Runnable),
postAtTime(Runnable, long),
postDelayed(Runnable, Object, long),
sendEmptyMessage(int),
sendMessage(Message), 
sendMessageAtTime(Message, long),
sendMessageDelayed(Message, long).

Looper:

Whatever I mentioned earlier is supported by Looper It has a loop() method which keeps running and listening for the new messages, main thread has one running all the time so you can receive messages from another thread to this thread, if you are creating your own thread and want to listen then don't forget to call prepare() in the thread that is to run the loop.

一个简单的例子

     class LooperThread extends Thread {
      public Handler mHandler;

      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
      }
  }

所以现在回答你的问题:

1: 事物(线程等 Looper 和 Handler)保持 运行 直到它被用户完成或被系统杀死。

2: Looper 有一个 loop() 方法,它会处理队列中的每条消息,并在队列为空时阻塞。

3:如果要从后台线程更新 UI,请确保应用程序在前台或在 onDestroy() 中终止后台线程,您可以使用 handler.getLooper().quitSafely() 或退出循环处理消息handler.looper.quit() 如果处理程序附加到主线程。

建议在方向更改或其他配置更改的情况下确保终止后台线程。

What happens to these tasks when android application gone to background?

它将按预期工作,UI 的行为没有变化。唯一的问题是我们无法看到这些变化。

最佳解决方案: 当应用程序处于后台时更新处理程序中的任何视图

例如:TextView 并在其中设置一些文本,并在应用程序进入前台后获取此 TextView 值。

Will these tasks are processed even in the background? Will the complete UI thread be suspended in background? Or? All the tasks posted to UI thread are suspended?

正如我上面提到的,此任务将按原样运行。

当您移至后台时应用程序不会更改UI线程,它将保持原样。

What happens to UI thread when there are no tasks posted to it after the activity is completely loaded? Will it be suspended, if there are no tasks are in the Looper?

简单来说就是Looper线程,比如UI线程。这样的线程有自己的 Looper,它为线程运行消息循环。消息队列完成后,它将自行完成。

UI 线程不会自行挂起,除非您编写一些代码来更改它:)。

很高兴阅读: Androids Handler.post, what happens exactly

What is the relationship between Looper, Handler and MessageQueue in Android?

这个案子有很多答案。我会尽量准确地回答问题。

What happens to these tasks when android application gone to background?

没有任何中断。 Handler 将在指定时间 MessageQueue 上 post 一个 Message(除非托管进程被终止)。

Will these tasks are processed even in the background?

Handler的角度来看:它不知道应用程序是在前台还是后台。它所知道的是,它应该 post 一个 Message 一段时间后。

Will the complete UI thread be suspended in background? Or? All the tasks posted to UI thread are suspended?

What happens to UI thread when there are no tasks posted to it after the activity is completely loaded? Will it be suspended, if there are no tasks are in the Looper?

UI 线程在任何情况下都不会 “暂停”(除非托管进程被终止)。只是由于 MessageQueue 为空,它会空闲 - 因此不需要完成任何工作。

附带说明一下,当应用程序进入后台时,请避免执行 UI 相关操作,因为视图层次结构的状态将无法正确保存。假设当应用程序处于后台时,您一次执行了 textView.setText("bob")。现在,这个 TextView 的状态将不会被保存,因为 onSaveInstanceState() 已经被执行并且不会再次执行,因此如果应用程序的进程被终止并重新创建(即作为结果系统资源不足),那么 activity 将恢复到调用 onSaveInstanceState() 时的状态。现在用户不会看到 "bob".

您可以使用 Handler#removeCallbacks() API.

取消已安排的活动