您的位置:首页 > 移动开发 > Android开发

android中线程之间的信息传递

2011-06-07 17:26 387 查看
Handler

java.lang.Object
android.os.Handler
Known Direct Subclasses

AsyncQueryHandler, AsyncQueryHandler.WorkerHandler, HttpAuthHandler, SslErrorHandler

原意:

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允许你去发送并且处理信息而且Runnable接口对象是和我们的一个线程MessageQueue相联系的。每个Handler实例与单个线程,该线程的消息队列相关联的。当您创建一个新的Handler,它是绑定到线程/消息的线程产生了这个队列 -从这点上来看,它会提供信息,并到该消息队列的Runnable并执行他们,因为他们来的消息出来队列。

Handler的主要作用:

Handler主要是用来跟UI主线程交互用。 比如:
1、你用handler发送一个message,然后在handler的线程中来接收、处理该消息,以避免直接在UI主线程中处理事务导致影响UI主线程的其他处理工作。
2、你可以将handler对象传给其他进程,以便在其他进程中通过handler给你发送事件。
3、通过handler的延时发送message,可以延时处理一些事务的处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: