【Android翻译】Message、MessageQueue、Handler与Looper

自己翻译的,英语烂,所以保留原文。如果有人看,还请结合原文理解吧。

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.

定义一个可以被发送到Handler并且包含"一个描述和一个任意数据对象"的Message对象。这个对象包含两个int类型的field和一个Object类型的field,它们在许多情况下允许你不为它们赋值。

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.

当Message类的构造方法是public时,得到一个Message对象的最佳实践方式就是去调用Message.obtain()或者某个Handler.obtainMessage()方法,这样得到的对象就是从可重用对象池中获取的。

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 through Handler objects associated with the Looper. You can retrieve the MessageQueue for the current thread with Looper.myQueue().

持有被Looper发送的那些消息的清单的“低层”类。
那些Message对象不是直接添加到MessageQueue中的,
而是通过与Looper相关联的Handler对象来添加的。
你可以通过Looper.myQueue()方法重新获取当前线程的MessageQueue。

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.

一个Handler对象允许你发送和处理Message和Runnable对象(此处的Message和Runnable对象,与一个线程的MessageQueue相关)。每个Handler实例都与单个线程和这个线程的Message Queue相关联。当你创建一个新的Handler,它会跟与之关联的线程进行绑定,会跟那个线程创建的Message Queue进行绑定——从绑定时开始,它就会传递Message和Runnable对象给Message Queue,并在它们从Message Queue出来时处理它们。

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.

Handler的主要作用有两个:
1.处理将来会被执行的Message和Runnable对象;
2.为相对于你自己所在线程的另一个线程上的操作入列(把Message送到MessageQueue)。

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 post versions 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).

post(Runnable)postAtTime(Runnable, long)postDelayed(Runnable, long)sendEmptyMessage(int)sendMessage(Message)sendMessageAtTime(Message, long) 还有sendMessageDelayed(Message, long)等方法完成对Message对象的调度。
那些带“post”字眼的方法会在它们接收到要被MessageQueue调用的Runnable对象后将它们添加到MessageQueue;那些带“send”字眼的方法会将带一系列数据的Message对象添加到MessageQueue,这些Message对象会在Handler的handleMessage()方法被处理(要求你实现一个Handler的子类)。

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.

当post或者send到Handler时,你既可以让数据在MessageQueue准备好时马上就处理这些Message或者Runnable对象,也可以指定它们被处理前要等待的时间,又或者指定一个它们被处理的确切时间。后两个操作可以让你实现各种基于时间的操作。

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.

当系统为你的应用程序开启了一个进程,它的主线程会专注于运行一个MessageQueue,这个MessageQueue负责管理那些顶层的应用程序对象(Activity、BroadcastReceiver等等)和它们创建的任何窗口。你可以创建你自己的线程,通过一个Handler让你的线程跟应用程序主线程进行通信。这个通信操作一样是像上面描述那样通过“post”或者“send”的方法来完成的,不过是从你自己的线程中“post”、“send”而已。那些传出去的Runnable或者Message对象会被添加到Handler的MessageQueue对象并在适当的时间被处理。

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.

Looper是用于为一个线程运行一个消息循环的类。线程在默认情况下没有与之相关的消息循环;要创建一个Looper,在需要运行消息循环的线程中调用 “prepare()” 方法,然后调用 “loop()” 方法去让它处理消息,知道循环结束。大部分与消息循环有关的交互是通过Handler类进行的。下面是一个实现了Looper线程的典型例子,在“prepare()” 和“loop()”方法之间创建一个Handler实例去与Looper进行沟通。

  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();
      }
  }

你可能感兴趣的:(【Android翻译】Message、MessageQueue、Handler与Looper)