android---handler

java的handler类是一个抽象类,android的handler是一个普通的类

public class Handler extends Object

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

 

Class Overview

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允许你发送和处理消息和一个线程对象与一个线程对象的消息队列相联系。

每一个handler实例与一个单线程还有该线程的消息队列相联系。

当你创建一个handler,被绑定在该线程和它的消息队列上。

传送消息,进入消息队列,执行当他们走出消息队列。

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调度消息,将来执行

                                                  2把一个action加入队列在不同的线程中执行

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

这里主要是介绍handler函数作用。

post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message),sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long)

这些函数主要是将线程,消息加入消息队列

执行在handler子类的handleMessage(message)函数

目前我看到网上的一些例子都是handler的子类在处理消息,handler不是一个普通类吗,又不想java的handler一样是一个抽象类,为什么就不能直接使用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.

当你post一个线程或者发送一个消息,你可以马上处理只要消息队列准备好了,也可以在某个时间点执行,或者delay执行

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 orsendMessage methods as before, but from your new thread. The given Runnable or Message will than be scheduled in the Handler's message queue and processed when appropriate

当你的应用程序创建了处理,主线程还有消息队列用于关系顶级的应用程序对象如activities, broadcast receivers,还有窗口创建。你可以创建自己的线程,和主线程交互通过handler。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值