android thread Handler 、Looper、 Message、 Message Queue

本文深入探讨了Android应用中Handler的使用方法,包括如何发送和处理Message和Runnable对象,以及如何利用Looper运行消息循环。重点阐述了Handler的两个主要用途:规划未来执行的消息和Runnable,以及将操作安排在其他线程执行。

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

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.

//handle允许你发送、处理和线程 MessageQueue相关的Message和Runnable对象

//当你创建了一个handle,handle就绑定到了这个thread和他的Message queue,  所以这个handle就可以投递messages和runnables给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. 

//handle的两个主要的用处

//(1  来规划messages和runnables在将来的某一时刻能够被执行

//(2 给一个action排进queue让其在你的其他thread中被执行



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对象在message queue中排序

//sendMessage 版本允许你 的 一个包含数据的Message对象 能够被Handler 的handleMessage(Message)函数处理


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.

//如果message queue可以的话你可以允许你的项目被快速执行,或者指明一个特定的延迟或相对时间来处理。<-后面这两项允许你来实现超时,ticks,和其他以时序为基础的行为。

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. 

//当你为你的程序创建一个process的时候,process的主thread就会去跑一个能够维护高层次的程序对象(如activities,broadcast receiver,等等)和任何他们创建的windows的message queue。你可以通过handler使你自己创建的thread和主线程通信。



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 a Looper thread, using the separation ofprepare() andloop() to create an initial Handler to communicate with the 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();
      }
  }


 



Message


Class Overview


Defines a message containing a description and arbitrary data object that can be sent to aHandler. 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 callMessage.obtain() or one of theHandler.obtainMessage() methods, which will pull them from a pool of recycled objects.



MessageQueue


Class Overview


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.

//MessageQueue有一个通过Looper遣送messages的messages列表


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



其他好的文章

1)http://www.cnblogs.com/xirihanlin/archive/2011/04/11/2012746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值