Handler概述

原文地址:http://blog.csdn.net/wcs542882916

/**

 * 一个Handler允许你去发送和处理Message和Runnable对象,

 * 而Message和Runnable对象是与一个线程的MessageQueue相关的。

 * 每个Handler实例与一个单独的线程相关且和这个线程的MessageQueue相关。

 * 当你创建一个Handler的时候,这个Handler会绑定到创建这个Handler的线程和线程的MessageQueue。

 * 从创建Handler开始,Handler将会发送Message 和Runnable到对应的MessageQueue,

 * 并且在Message和Runnable被移出MessageQueue的时候处理。

 *

 *Handler有两个主要用法:

 * (1)计划在将来的某个时间点处理Message和Runnable。

 * (2)在不同的线程里将一个动作加入Handler所对应的队列去执行,这个线程不只是创建Handler的线程。

 *

 * 计划Message用以下这些方法来完成,#post,#postAtTime(Runnable,long),

 *#postDelayed,#sendEmptyMessage,#sendMessage,

 *#sendMessageAtTime,#sendMessageDelayed。

 * 这个post版本的方法允许你把Runnable对象加入队列,在Runnable对象移出的时候被MessageQueue调用;

 * 这个sendMessage版本的方法允许你把Message对象(Message对象里包含一些数据)加入队列,

 * 并且被Handler的#handleMessage方法处理,

 * 这个sendMessage版本需要你实现Handler的子类,重写#handleMessage方法。

 *

 * 当posting 或 sending

 * 给Handler时,你能允许这item在MessageQueue准备好后及时的被处理或者指定一个延迟时间,

 * 又或者指定一个绝对时间点。

 *#sendMessageAtTime,#sendMessageDelayed这两个方法允许你实现延时,定时,和其它基于定时的操作。

 *

 * 当你的应用进程被创建的时候,应用的主线程就一直致力于运行一个MessageQueue,

 * 主线程致力于管理顶级应用对象(activity,broadcast receivers,etc) 和任何创建的窗口。

 * 你可以创建你自己的线程,然后通过Handler与应用主线程通信。这种与主线程的通信方式被实现,

 * 也是像之前一样同样通过post和sendMessage方法。

 *Runnable对象和Message将要被加入Handler的MessageQueue中,在合适的时候被处理。

 *

 * AHandler allows you to send and process{@link Message} and Runnable objects

 *associated with a thread's {@linkMessageQueue}. 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 -- fromthat 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

 * tobe executed as some point in the future; and (2) to enqueue an action to

 * beperformed on a different thread than your own.

 *

 *Scheduling messages is accomplished with the {@link #post},

 *{@link #postAtTime(Runnable, long)}, {@link #postDelayed},

 *{@link #sendEmptyMessage}, {@link #sendMessage}, {@link #sendMessageAtTime},

 *and {@link #sendMessageDelayed} methods. The post versions allow you

 * toenqueue Runnable objects to be called by the message queue when they are

 *received; the sendMessage versions allow you to enqueue a

 *{@link Message} object containing a bundle of data that will be processed by

 *the Handler's {@link #handleMessage} 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

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

 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值