【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();
      }
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,548评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,069评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,985评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,305评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,324评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,030评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,639评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,552评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,081评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,194评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,327评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,004评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,688评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,188评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,307评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,667评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,337评论 2 358

推荐阅读更多精彩内容