java handler类_Handler类介绍(中文文档)

Handler是Android中用于处理子线程与主线程交互的重要组件,它允许在子线程中发送消息到主线程,从而更新UI。Handler有两个主要功能:1. 安排任务在未来某个时刻执行;2. 在不同线程间进行通信。通过post或sendMessage方法,可以在子线程中向Handler发送Runnable或Message对象,由主线程处理并更新UI。在创建Handler时,可以指定一个Callback接口来处理Message。此外,Handler提供了各种方法用于调度消息,如postDelayed、sendMessageAtTime等,实现定时或延迟执行任务。
摘要由CSDN通过智能技术生成

Handler用法

一、Handler的定义

主要接受子线程发送的数据,并用此数据配合主线程更新UI。

当应用程序启动时,Android首先会开启一个主线程 (即UI线程),主线程管理界面中的UI控件,进行事件分发,比如说:点击Button,Android系统会分发事件到Button上,来响应你的操作。如果此时需要一个耗时的操作,例如: 联网读取数据,或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,如果你放在主线程中的话,界面会出现假死现象,如果5秒钟还没有完成的话,会收到Android系统的一个错误提示“强制关闭”。这个时候我们需要把这些耗时的操作,放在一个子线程中。

因为子线程涉及到UI更新,Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新,子线程中操作是危险的。这个时候,Handler就出现了。来解决这个复杂的问题,由于Handler运行在主线程中(UI线程中), 它与子线程可以通过Message对象来传递数据,这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传递Message对象,(里面包含数据),把这些消息放入主线程队列中,配合主线程进行更新UI。

二、Handler一些特点

public class Handler extends Object

java.lang.object à android.os.Handler

已知直接子类:

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允许你发送和处理Message(消息)和与一个线程相关的MessageQueue(消息队列)的Runnable对象,每一个Handler实例都与一个单独的线程和该线程的消息队列相关。当你创建一个新的Handler,它被绑定到创建它的线程/消息队列线程中。从这一点上来说,它将传递消息和runnable对象,并且执行他们,因为他们来自于消息队列中。

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、安排messages和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).

安排messages是通过 post(Runnable), postAtTime(Runnable, long),

postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long)方法来完成的。The post versions允许你排列Runnable对象被接收到的消息队列所调用;the sendMessage versions允许您排列一个包含了大量将被Handler的handleMessage(Message)方法处理的数据的Message对象(需要你实现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.

当posting或sending一个Handler,您可以允许item当message对列一准备就绪就进行处理,你也可以在它被处理之前指定一个延迟或绝对时间对它进行处理。后两个允许您实现超时和其他以时序为基础的行为。

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 s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值