Android Handler

来源:互联网 发布:成都软件学院 编辑:程序博客网 时间:2024/05/01 10:22


参考:
http://developer.android.com/reference/android/os/Handler.html


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允许发送并处理 与一个线程的消息队列(MessageQueue)相关连的消息(Message)或 Runnable对象。 每个Handle实例与一个线程以及该线程的消息队列相关联。 当创建一个Handler时,它与创建的线程及该线程的消息队列绑定。 从Handler创建开始,Handler将分发消息(Message)和Runnable对象到线程的消息队列, 这些消息或Runnable对象在他们从消息队列中出来的时候得到执行。


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、调度消息或runable对象,使其在未来的某个时间点得到执行。
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 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) 
 post版本的函数将runnable入队,在消息队列收到这些runnable对象时,将执行这些runnable对象。
 sendMessage版本的函数将 带有bundle数据的消息(Message)入队。这些消息将由Handler的 handleMessage(Message)
 函数执行(需要实现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的时候,可以允许消息队列尽快处理消息,或延时处理,或在固定的时间处理。
 后面的两种方式可以用来实现超时、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 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. 
 当为某个应用创建一个进程时,主线程负责运行一个消息队列,该消息队列负责管理顶层
 的应用对象(activies, broadcast receivers等) 和 任何创建的窗口。你可以创建你自己的线程, 并且
 通过 Handler 与主线程进行通信, 这也是通过sendMessag方法实现的。但是,对于你的新线程, 这些Runnable
 或消息将在 Handler的消息队列(即主线程中)中得到调度并在适当时候得到处理。

0 0
原创粉丝点击