Handler

来源:互联网 发布:大数据公司的经营范围 编辑:程序博客网 时间:2024/06/05 15:59

Class Overview


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对象。每个Handler实例与单个线程和线程的消息队列相关联。当你创建一个新的Handler,它绑定到线程/线程的消息队列创建它——从那时起,它将提供messages和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)排列一个动作与你自己的一个不同的线程上执行。

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)方法。发布版本允许你在接收到时Runnable对象称为消息队列;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. 发布或发送到Handler时,您可以允许项目尽快处理准备这样做的消息队列,或在相对或绝对的处理时间之前指定一个延时。后两个允许您实现超时,标记,和其他基于时间的行为。

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. 为应用程序创建进程时,其主线程致力于运行一个负责管理顶级应用程序对象(活动、广播接收器等)和他们创建的任何窗口的线程消息队列。您可以创建自己的线程,并且通过一个Handler与主应用程序线程通信。这是和之前一样通过调用相同的post或sendMessage方法,但从你的新线程开始。给定的Runnable或Message将被安排在Handler的信息队列,并在适当的时候处理。

0 0
原创粉丝点击