翻译04

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

Handler

extends Object
java.lang.Object   ↳android.os.HandlerKnown Direct Subclasses
AsyncQueryHandlerA helper class to help make handling asynchronousContentResolver queries easier. AsyncQueryHandler.WorkerHandler HttpAuthHandlerRepresents a request for HTTP authentication. SslErrorHandlerRepresents a request for handling an SSL error. 

Class Overview


A Handler allows you to send and process Message and Runnable objects associated with a thread'sMessageQueue. 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.

Scheduling messages is accomplished with the post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long),sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long), andsendMessageDelayed(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'shandleMessage(Message) method (requiring that you implement a subclass of 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.

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

 
 
译文:
一个处理程序允许您发送和处理消息和Runnable对象与线程的MessageQueue相关联。每个处理程序实例相关联单个线程和线程的消息队列。当你创建一个新的管理器,就创立了一个绑定到线程的消息队列。当他们出队列的时候,它可以传递、捕获、执行消息队列。管理器有两种主要用途。①在指定的时间执行和捕获消息。②讲Action进行摆列,使其在不同的线程上执行。
调度信息完成后可执行post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), 和 sendMessageDelayed(Message, long)的方法。发布版本允许您将Runnable对象被称为当他们收到的消息队列。sendMessage版本允许您将一个消息对象包含一个包的数据将被处理程序处理的handleMessage(消息)方法(处理程序要求您实现一个子类)。
当发布或发送一个管理器时,你即可以允许项目尽快处理准备好的消息队列,也可以延迟一个队列的指定时间。后两个允许你实现超时,运转还有其他timing-based行为。
当一个进程为你的应用程序创建后,它的主线程用于运行一个负责管理高层应用程序消息队列和他们创建的任何窗口。你可以创建你自己的线程。将一个处理程序寓主应用程序线程相互通信。通过调用相同的post和sendMessage方法,而不是你自己的新线程。给定的Runnable或消息将被安排在处理程序的消息队列,并在适当的时候处理。
0 0
原创粉丝点击