Android开发文档翻译(Hanlder Looper Message MessageQueue)

1.Handler:

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. 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.

一个处理程序允许您发送和过程消息和可运行 与线程相关联的对象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), and sendMessageDelayed(Message, long) methods. The postversions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).

完成的调度信息帖子(可运行)postAtTime(Runnable,长)postDelayed(Runnable,长)sendEmptyMessage(int)sendMessage(消息)sendMessageAtTime(消息,长),sendMessageDelayed(消息,长)方法。 的文章版本允许 你排队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 same post or sendMessage 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

当一个 过程是为您的应用程序创建的,其主要线程致力于 运行一个消息队列,负责管理高层 应用程序对象(活动、广播接收器等)和任何窗口 他们创建。 您可以创建自己的线程,并沟通 主应用程序线程通过一个处理程序。 这是通过调用来完成的 相同的文章或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, call prepare() in the thread that is to run the loop, and then loop() 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 a Looper thread, using the separation of prepare() and loop() to create an initial Handler to communicate with the Looper.

类用于运行一个消息循环的线程。 线程默认情况下做 没有一个消息循环与之相关;创建一个电话准备()在运行的线程循环,然后循环()它处理消息,直到停止循环。

大多数与一个消息循环是通过交互处理程序类。

这是一个典型的例子,缝合线的实现, 使用的分离准备()循环()创建一个 最初的处理程序与电影进行通信。

 

3.Message:

Defines a message containing a description and arbitrary data object that can be sent to a Handler. This object contains two extra int fields and an extra object field that allow you to not do allocations in many cases.

While the constructor of Message is public, the best way to get one of these is to call Message.obtain() or one of the Handler.obtainMessage() methods, which will pull them from a pool of recycled objects.

定义一个消息包含一个描述和可以任意数据对象 发送到处理程序。 这个对象包含两个额外的int字段和一个 额外的对象字段,在很多情况下不允许您配置。

虽然消息的构造函数是公共的,最好的办法 其中一个是调用Message.obtain()或其中一个Handler.obtainMessage()方法,将拉 他们从池中回收对象。

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.

You can retrieve the MessageQueue for the current thread with Looper.myQueue().

低级类控股派出的消息列表电影。 消息不是MessageQueue直接添加, 而是通过MessageQueue.IdleHandler与电影相关的对象。

您可以检索的MessageQueue当前线程Looper.myQueue()

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值