类概述
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.
一个处理程序允许您发送和实现Message
进程,以及实现有关于MessageQueue的Runnable 对象。每个处理程序实例都与线程和线程之间的消息队列相关
。当您创建一个新的处理程序时,它必然是创建它的线程所需的线程/消息队列——从这一点上看,它将提供信息和Runnables对象,形成消息队列以及执行他们出来的消息队列。
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)计划消息和在将来的某些点上执行的Runnables对象;(2)将执行一个与handler本身不同的线程。
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对象称作当被收到时被称为消息队列;SendMessage版本允许您将一个包含的数据包将被处理的handleMessage(Message)
处理成为一个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.
当粘贴或者发送到一个处理程序时,您可以允许项目尽快消息队列来准备这样做加工 ,或指定一个延迟之前得到处理或绝对时间对它进行处理。或者允许你实现超时和其他基于时序的行为。
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.
当一个进程是应用程序的主线程创建的,它专门运行消息队列,负责管理顶级应用程序对象(活动,广播接收器,等)和他们创建的任何windows。您可以创建属于您自己的线程,回到主应用程序线程通过经理人沟通。这是通过调用同一岗位或SendMessage方法之前做的,而不是从你的新的线程。给定的运行或消息将被安排在处理程序的消息队列和处理,在适当的时候。