您的位置:首页 > 产品设计 > UI/UE

android thread Handler 、Looper、 Message、 Message Queue

2014-10-07 08:48 344 查看
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.

//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)
, and
sendMessageDelayed(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 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对象在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, 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.

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 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.

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
through
MessageQueue.IdleHandler
objects associated with the Looper.

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

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

其他好的文章

1)/article/4907558.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐