Android-Looper类介绍
2014-03-08 19:05
316 查看
Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理。
Handler其实可以看做是一个工具类,用来向消息队列中插入消息的。
(1) Looper类用来为一个线程开启一个消息循环。
默认情况下android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息
循环。)
Looper对象通过MessageQueue来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue。
(2) 通常是通过Handler对象来与Looper进行交互的。Handler可看做是Looper的一个接口,用来向指定的Looper发送消息及定义处理方法。
默认情况下Handler会与其被定义时所在线程的Looper绑定,比如,Handler在主线程中定义,那么它是与主线程的Looper绑定。
mainHandler = new Handler() 等价于new Handler(Looper.myLooper()).
Looper.myLooper():获取当前进程的looper对象,类似的Looper.getMainLooper()
用于获取主线程的Looper对象。
(3) 在非主线程中直接newHandler()
会报如下的错误:
E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due touncaught exception
E/AndroidRuntime( 6173): java.lang.RuntimeException: Can't create handlerinside thread that has not called Looper.prepare()
原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。
(4) Looper.loop();
让Looper开始工作,从消息队列里取消息,处理消息。
注意:写在Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。
(5) 基于以上知识,可实现主线程给子线程(非主线程)发送消息。
把下面例子中的mHandler声明成类成员,在主线程通过mHandler发送消息即可。
* Android官方文档中Looper的介绍:
Class used to run a message loop for a thread. Threads by default do not have amessage loop associated with them; to create one, call prepare() in the threadthat is to run the loop, and then loop() to have it process messages
until theloop is stopped.
Most interaction with a message loop is through the Handler class.
This is a typical example of the implementation of a Looper thread, using theseparation of prepare() and loop() to create an initial Handler to communicatewith the Looper.
Handler其实可以看做是一个工具类,用来向消息队列中插入消息的。
(1) Looper类用来为一个线程开启一个消息循环。
默认情况下android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息
循环。)
Looper对象通过MessageQueue来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue。
(2) 通常是通过Handler对象来与Looper进行交互的。Handler可看做是Looper的一个接口,用来向指定的Looper发送消息及定义处理方法。
默认情况下Handler会与其被定义时所在线程的Looper绑定,比如,Handler在主线程中定义,那么它是与主线程的Looper绑定。
mainHandler = new Handler() 等价于new Handler(Looper.myLooper()).
Looper.myLooper():获取当前进程的looper对象,类似的Looper.getMainLooper()
用于获取主线程的Looper对象。
(3) 在非主线程中直接newHandler()
会报如下的错误:
E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due touncaught exception
E/AndroidRuntime( 6173): java.lang.RuntimeException: Can't create handlerinside thread that has not called Looper.prepare()
原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。
(4) Looper.loop();
让Looper开始工作,从消息队列里取消息,处理消息。
注意:写在Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。
(5) 基于以上知识,可实现主线程给子线程(非主线程)发送消息。
把下面例子中的mHandler声明成类成员,在主线程通过mHandler发送消息即可。
* Android官方文档中Looper的介绍:
Class used to run a message loop for a thread. Threads by default do not have amessage loop associated with them; to create one, call prepare() in the threadthat is to run the loop, and then loop() to have it process messages
until theloop is stopped.
Most interaction with a message loop is through the Handler class.
This is a typical example of the implementation of a Looper thread, using theseparation of prepare() and loop() to create an initial Handler to communicatewith the Looper.
相关文章推荐
- android下Rotate旋转动画实现效果
- android实现translate位移动画效果
- android实现scale伸缩动画效果
- Android 开发Project中各个目录和文件的介绍
- Android基础课程-创建第一个应用
- android实现alpha渐变动画效果
- 基础总结篇之二:Activity的四种launchMode
- android ImageView 详解------API介绍(一)
- Android RadioGroup 换行, 支持多行多列
- genymotion- 快速的android模拟器
- Android问题集锦之二十:Activity has leaked window that was originally added
- android中slidemenu的开源项目的学习
- Android 亲测源码分享
- 获取Android签名证书的sha1值
- Android
- 【android学习】_如何创建一个android下可以使用的数据库
- Android学习小Demo(12)TodoList实现ListView的分组实现
- 如何导进开源库StickyListHeaders
- android 手机定位小测试工程
- Android 中的telephony