什么是活套,处理程序和消息队列之间的关系?队列、消息、关系、程序

2023-09-12 10:45:09 作者:择一人守一生

我是新来的机器人,并得到了非常困惑这些概念。我查了官方指南,还是didnt完全得到它。什么是活套,处理程序和消息队列之间的关系?

I am new to android, and got very confused with those concepts. I have check the official guide, still didnt quite get it. what is the relationship between Looper, Handler and Message queue?

推荐答案

A 尺蠖是一个消息处理循环:它读取和从 MessageQueue 。该尺蠖类通常是在发的 LooperThread (子类)。

A Looper is a message handling loop: it reads and processes items from a MessageQueue. The Looper class is usually used in conjunction with a LooperThread (a subclass of Thread).

A 处理程序是一种有助于与尺蠖交互的工具类—主要是通过发布消息和的Runnable 反对线程的 MessageQueue 。当处理程序创建,它绑定到特定尺蠖(以及相关的线索和消息队列)。

A Handler is a utility class that facilitates interacting with a Looper—mainly by posting messages and Runnable objects to the thread's MessageQueue. When a Handler is created, it is bound to a specific Looper (and associated thread and message queue).

在典型的用法,创建并启动 LooperThread ,然后创建一个处理程序对象(或对象)由哪些其他线程可以使用 LooperThread 实例进行交互。该处理程序必须同时运行创建的 LooperThread ,虽然曾经创造了有哪些线程可以使用没有任何限制处理程序的调度方式(后(可运行),等等。)

In typical usage, you create and start a LooperThread, then create a Handler object (or objects) by which other threads can interact with the LooperThread instance. The Handler must be created while running on the LooperThread, although once created there is no restriction on what threads can use the Handler's scheduling methods (post(Runnable), etc.)

在一个Android应用程序的主线程(也称为UI线程)设置为你的应用程序创建前弯针线。

The main thread (a.k.a. UI thread) in an Android application is set up as a looper thread before your application is created.

除了类文档,有这一切的一个很好的讨论这里。

Aside from the class docs, there's a nice discussion of all of this here.