Hanlder Looper Message MessageQueue

本文深入解析了Android消息机制中的核心组件——Handler、Looper、Message和MessageQueue。详细介绍了它们的作用及工作原理,并提供了典型的应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Hanlder:

A Handler allows you to send and process Message and Runnable objects associated with a thread'sMessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.一个处理程序允许您发送和处理的消息和Runnable对象以及线程的MessageQueue相关联。每个处理程序实例和单个线程和线程的消息队列有关系。当你创建一个新的处理程序,它绑定到线程的消息队列,它将提供消息和runnables消息队列并且执行他们的消息队列。

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.处理程序有两个主要用途:(1)在将来的某个时间点调度信息和执行runnables(2)在你自己的一个不同的线程上执行排列的动作。

Scheduling messages is accomplished with the post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long),sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long), andsendMessageDelayed(Message, long) methods. Thepost versions allow you to enqueue Runnable objects to be called by the message queue when they are received; thesendMessage versions allow you to enqueue aMessage object containing a bundle of data that will be processed by the Handler'shandleMessage(Message) method (requiring that you implement a subclass of Handler).调度信息由post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long),sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long), andsendMessageDelayed(Message, long)方法完成。发布版本允许您将收到被称为由消息队列Runnable对象时;sendMessage版本允许您将一个包含一个包的数据的消息对象被处理程序处理的handleMessage(消息)方法(处理程序要求您实现一个子类)。

When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior. 发布或发送到处理程序时,您可以允许项目尽快处理消息队列,或指定一个延迟之前加工处理或绝对时间。后两个允许您实现超时,运转,和其他timing-based行为。

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 samepost orsendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.为应用程序创建进程时,其主要致力于运行一个线程消息队列,负责管理顶级应用程序对象(活动、广播接收器等)和他们创建的任何windows。您可以创建自己的线程,并且与主应用程序线程通信通过一个处理程序。这是通过调用相同的帖子或sendMessage方法和之前一样,但你的新线程给定的Runnable或消息将被安排在处理程序的消息队列,并在适当的时候处理。

2、Looper:

Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, callprepare() in the thread that is to run the loop, and thenloop() to have it process messages until the loop is stopped.类用于运行一个消息循环的线程。线程默认情况下不具有与其相关联的一个消息循环;创建一个打电话准备()运行循环的线程,然后循环()来处理消息,直到停止循环。

Most interaction with a message loop is through the Handler class.大多数交互消息循环是通过处理程序类。

This is a typical example of the implementation of , using the separation of to create an initial Handler to communicate with the Looper.这是一个典型的例子, Looper thread的实现,使用分离的prepare() andloop()来创建一个初始处理程序与Looper进行通信。

3、Message:

Reference to a Handler, which others can use to send messages to it. This allows for the implementation of message-based communication across processes, by creating a Messenger pointing to a Handler in one process, and handing that Messenger to another process.引用一个处理程序,其他人可以使用它来发送消息。这允许实施的基于消息的通信流程,通过创建一个信使指着一个处理程序在一个过程中,和将信使交给另一个进程。

4、MessageQueue:

Low-level class holding the list of messages to be dispatched by a Looper. Messages are not added directly to a MessageQueue, but rather throughMessageQueue.IdleHandler objects associated with the Looper.底层阶级的Looper是一系列派出的消息列表。消息不是直接添加MessageQueue,而是通过MessageQueue.IdleHandler与Looper相关的对象。

You can retrieve the MessageQueue for the current thread with Looper.myQueue().您可以检索当前线程的MessageQueue Looper.myQueue()。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值