`
xuela_net
  • 浏览: 494880 次
文章分类
社区版块
存档分类
最新评论

Handler 消息传递机制

 
阅读更多

1,Handler 的概念
Handler 是用来干什么的?
1)执行计划任务,可以在预定的时间执行某些任务,可以模拟定时器

2)线程间通信。在Android的应用启动时,会创建一个主线程,主线程会创建一个
消息队列来处理各种消息。当你创建子线程时,你可以在你的子线程中拿到父线程中
创建的Handler 对象,就可以通过该对象向父线程的消息队列发送消息了。由于Android
要求在UI线程中更新界面,因此,可以通过该方法在其它线程中更新界面。

角色描述:
1) Looper: 一个线程可以产生一个Looper对象,由它来管理此线程里的Message Queue

2) Handler: 你可以构造Handler对象来与Looper沟通,以便push 新消息到 Message Queue里,或者
接收Looper从Message Queue 里所送来的消息。

3)Message Queue(消息队列):是用来存放线程放入的消息。

4)线程:UI thread 通常就是 main thread,而Android 启动程序时会替它建立一个Message Queue。

每一个线程里可含有一个 Looper 对象以及一个 Message Queue 数据结构。在你的应用程序里,
可以定义 Handler 的子类别来接收 Looper 所送出的消息。

2,Handler 的执行过程

每个handler 对应一个线程 thread , 在子线程中 handler 发送的消息会进入到 Message Queue当中去,由 looper 再来分发给 Handler 处理。


3,Handler 异步实现方法

见实例

http://download.csdn.net/detail/fulinwsuafcie/4437306


4,Handler 与线程的关系

Handler 一般运行于主线程内

问题:
1,使用Handler 是异步的,它会建立新的线程么? 不会
2,Handler 是在主线程内么? 一般都会在主线程内,但也可以在子线程中创建Handler
3,Handler 的 post 和 sendMessage 方法,使用的是一个队列不安是两个? 一个
4,子线程中建立一个 handler, 然后sendMessage 会怎么样? 会崩溃
5,子线程建立 handler ,构造的时候舒心入主线程的 Looper ? yes

如果有兴趣的话,可以看下原始的android 注释,摘自 Handler.java 代码的头部注释。

/**
* A Handler allows you to send and process {@link Message} and Runnable
* objects associated with a thread's {@link 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.
*
* <p>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.
*
* <p>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 <em>post</em> versions allow
* you to enqueue Runnable objects to be called by the message queue when
* they are received; the <em>sendMessage</em> 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).
*
* <p>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.
*
* <p>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 <em>post</em> or <em>sendMessage</em> 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.
*/

分享到:
评论

相关推荐

    Android 的Handler消息传递机制.pdf

    Android 的Handler消息传递机制.pdf 学习资料 复习资料 教学资源

    android计数器(Handler消息传递机制)

    程序功能说明:点击“开始”按钮后,文本框中的数字每隔1秒钟自动加1,当增加到100时就停止。 (Handler消息传递机制)

    17.Handler消息传递机制

    内含源代码。举例:在文本框中动态的生成一个随机产生得数。 1、尝试通过子线程改变主线程界面,动态生成随机数。

    Android中Handler消息传递机制

    主要介绍了Android中Handler消息传递机制 的相关资料,需要的朋友可以参考下

    学习Android Handler消息传递机制

    主要为大家详细介绍了Android Handler消息传递机制,感兴趣的小伙伴们可以参考一下

    Handler机制及原理探究.pdf

    Handler使用简单功能强大,常被用作线程间传递消息的组件,而且还可以用于跨进程。 消息机制背后有包括Looper ,MessageQueue管理和分发消息的实现,同时在Native层也单独实现了一套类 似的机制,接收和处理Native...

    详解android进行异步更新UI的四种方式

    使用Handler消息传递机制; 使用AsyncTask异步任务; 使用runOnUiThread(action)方法; 使用Handler的post(Runnabel r)方法; 下面分别使用四种方式来更新一个TextView。 1.使用Handler消息传递机制 package ...

    android学习文档

    3、Handler消息传递机制 36 4、Handler类的主要作用 37 六、Android实例 38 1、做多米播放器的特效 38 2、做时钟的特效 39 1. 涉及的知识 40 一、完成”时钟”应用程序的操作 43 七、图形与图像处理 44

    Handler主线程于子线程,子线程与子线程,主线程到子线程消息传递

    Handler消息传递详解,子线程到子线程,主线程到子线程,子线程到主线程 三种消息,Looper,Handler工作机制详解 https://blog.csdn.net/shoneworn/article/details/80447651

    Android事件处理

    3.1 Android事件处理概述 3.2 基于监听的事件处理 3.3 基于回调的事件处理 3.4 响应的系统设置的事件 3.5 Handler消息传递机制

    android 事件处理

    李刚疯狂android 第三章事件处理 包括android 的事件处理 基于监听的事件处理 基于回调的事件处理 响应的系统设置的事件 Handler 消息传递机制

    Android实例代码

    3.5、Handler消息传递机制: 第4章、深入理解Activity 4.1、建立、配置和使用Activity: 4.2、Activity的回调机制: 4.3、Activity的生命周期: 第5章、使用Intent和IntentFilter进行通信 5.1、Intent对象详解...

    疯狂Android讲义(第2版)源代码 第6章~第9章

    3.5、Handler消息传递机制: 第4章、深入理解Activity 4.1、建立、配置和使用Activity: 4.2、Activity的回调机制: 4.3、Activity的生命周期: 第5章、使用Intent和IntentFilter进行通信 5.1、Intent对象详解...

    ARM Cortex-A8和Android 4.x联动报警系统

    第8节:Handler消息传递机制.zip 第9节:Android定时器.zip 第10节:Android NDK入门.zip 第11节:Android NDK深入理论讲解.zip 第12节:Android NDK深入实例演示.zip 第13节:Android NDK深入实例演示2.zip 第14节...

    疯狂Android讲义源码

     3.5 Handler消息传递机制 166  3.5.1 Handler类简介 166  3.5.2 Handler使用案例 167  3.6 本章小结 168  第4章 深入理解Activity 169  4.1 建立、配置和使用Activity 170  4.1.1 建立Activity 170  4.1.2 ...

    疯狂Android讲义.part2

    3.5 Handler消息传递机制 166 3.5.1 Handler类简介 166 3.5.2 Handler使用案例 167 3.6 本章小结 168 第4章 深入理解Activity 169 4.1 建立、配置和使用Activity 170 4.1.1 建立Activity 170 4.1.2 配置Activity 177...

    疯狂Android讲义.part1

    3.5 Handler消息传递机制 166 3.5.1 Handler类简介 166 3.5.2 Handler使用案例 167 3.6 本章小结 168 第4章 深入理解Activity 169 4.1 建立、配置和使用Activity 170 4.1.1 建立Activity 170 4.1.2 配置Activity 177...

Global site tag (gtag.js) - Google Analytics