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.
一个管理者允许发送、处理信息,可以追踪目标线程的消息队列。每一个处理者案例只关联一个线程并且贯穿整个消息队列。当创建一个管理者时,它将被绑定到一个线程或一个已经创建的并指定的线性消息队列,发送消息、追踪消息队列,当它们从消息队列中出来时并执行。
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) 安排消息并追踪即将需要被执行的队列;(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允许队列化克追踪目标成为消息队列,发送消息可以插入一个绑定数据的信息,并且由管理者的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.
当声明或者发送一个管理者,就可以尽可能长久的使用已经准备好的消息队列,也可以在用资源前指定一个延迟或者应用资源的绝对时间。对于时间的设置,可以继承超时、 时间戳以及其他时间的行为。
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.
当创建了一个应用进程时,主线程其主导作用,管理应用程序线程最高线程(活动、广播接收器等等)和创建的活动窗体。我们可以自己创建自己的线程,通过管理者和主线程进行交互。相对于自己创建的线程来说,在这之前是通过调用同一岗位SendMessage方法来实现的。在适当的时候,这些被最终的消息将被安排到处理者的消息队列并执行。