Android学习笔记9——Handler初步

为什么需要Handler

我们不能将所有的操作都放在Activity 中进行。比如:当需要下载一个文件的时候,由于下载时间可能会比较长,而导致 Activity 处于一段较长的时间内无响应状态,用户体验不好,且时间太长的话, Activity 也会报错。


 

Handler的使用方法:

1、 创建一个Handler 的对象;

2、 当点击某按钮产生事件的时候,调用Handler post() 方法,将要执行的线程的对象添加到队列当中去;

3、 将要执行的操作写在线程对象的run() 方法中;

run() 方法内部执行 postDelayed() 或者是 post() 方法可以使线程循环执行下去。

 

实际上Handler 提供了一个异步的线程处理方法。即在 Activity 中将线程 post handler 的队列以后就返回了。这样我们要实现的操作就在新的线程里面运行而不影响到 Activity 的线程。

 

Android Reference中关于 Handler 的描述:

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

 

翻译如下:
    Handler允许你发送和处理消息(Message)和与一个消息队列(MessageQueue)相关联的Runnable对象。每个Handler的对象都与一个单独的线程及这个线程的消息队列相关联。当你创建一个新的Handler时,它将附着于创建它的线程或这个线程的消息队列——从这时开始,它将把消息和Runnable对象传递给消息队列并在它们出队的时候执行它们。
    Handler主要有两个用处:(1)调度消息和Runnable对象在未来某时刻的运行;(2)将一个不在本线程中执行的动作放入队列中。
    消息的调用由post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), 以及sendMessageDelayed(Message, long)方法来完成。post一类的方法允许你将被消息队列调用的Runnable对象放入队列中。sendMessage一类的方法允许你将包含一组将被handleMessage(Message)方法处理的数据的消息对象放入消息队列中(这要求你实现Handler的子类)*即重写Handler的handleMessage(Message)方法。
    当发布(post)或发送(send)对象给Handler,你可以使该对象在消息队列准备好时立即被执行,或者指定一个被处理的时延或被处理的绝对时间。后两者需要实现timeouts、ticks以及其他基于时间的行为。
    当你的程序创建一个进程的时候,它的主线程将被用于运行一个用于处理对顶级(top-level)应用程序对象(如activities、broadcast receivers等)和由它们创建的任意窗口的消息队列。你可以创建自己的线程,并通过Handler与主程序线程进行通讯。这通过你自己线程中的前面所述的post或sendMessage方法来完成。给出的Runnable对象或消息将在Handler的消息队列中调度并在适当时候进行处理。

 

一个Handler的例子(与ProgressBar结合):点击开始,进度条开始技术

 

源码:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值