Android 线程 Looper.prepare()、Looper.loop() 使用
Looper对象通过MessageQueue来存放消息和事件。一个线程仅仅能有一个Looper。相应一个MessageQueue。
下面是Android API中的一个典型的Looper thread实现:
//而默认情况下。线程是没有消息循环的,所以要调用 Looper.prepare()来给线程创建消息循环,然后再通过。Looper.loop()来使消息循环起作用。
- class LooperThread extends Thread
- {
- public Handler mHandler;
- public void run()
- {
- Looper.prepare();
- mHandler = new Handler()
- {
- public void handleMessage(Message msg)
- {
- // process incoming messages here
- }
- };
- Looper.loop();
- }
另,Activity的MainUI线程默认是有消息队列的。
所以在Activity中新建Handler时,不须要先调用Looper.prepare()。
那么遇到了有多Low的问题呢:
项目中重写了一个HandlerThread,然后定义了post方法。然后在主线程中例如以下实现:
AsyncHandler.post(new Runnable() {
@Override
public void run() {
try {
Looper.prepare();
// 一坨要异步运行的代码******
Looper.loop();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
那么明眼人一看就看出问题来了 ,这代码一跑异步代码肯定运行不到啊。为啥呢。且看下prepare的实现:
/** Initialize the current thread as a looper.
* This gives you a chance to create handlers that then reference
* this looper, before actually starting the loop. Be sure to call
* {@link #loop()} after calling this method, and end it by calling
* {@link #quit()}.
*/
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
}
So,简单,却是问题~
Android 线程 Looper.prepare()、Looper.loop() 使用的更多相关文章
- 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- 【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- Android消息队列和Looper
1. 什么是消息队列 消息队列在android中对应MessageQueue这个类,顾名思义,消息队列中存放了大量的消息(Message) 2.什么是消息 消息(Message)代表一个行为(what ...
- Android消息机制:Looper,MessageQueue,Message与handler
Android消息机制好多人都讲过,但是自己去翻源码的时候才能明白. 今天试着讲一下,因为目标是讲清楚整体逻辑,所以不追究细节. Message是消息机制的核心,所以从Message讲起. 1.Mes ...
- Android -- Looper.prepare()和Looper.loop() —深入版
Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理.handler其实可以看做是一个工具类,用来向消息队列中插入消息的. (1) Loope ...
- 【转】Android -- Looper.prepare()和Looper.loop()
Looper.prepare()和Looper.loop() 原文地址:http://blog.csdn.net/heng615975867/article/details/9194219 Andro ...
- Android -- Looper.prepare()和Looper.loop() —深度版
Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理.handler事实上能够看做是一个工具类.用来向消息队列中插入消息的. (1) Loop ...
- Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误
原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...
随机推荐
- [转]Android ListView的Item高亮显示的办法
本文转自:http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228601.html 在我们使用ListView的时候,经常会遇到某一项(Item ...
- 笨拙而诡异的 Oracle(之二)
有一张表,很多数据: 想取某个月的数据.初始的想法很简单,根据日期(RQ)形成条件即可: 符合条件的记录数是 129835,但耗时太长:14.515 秒(RQ字段是做过索引的)!直观的反应是 O ...
- SQL--大解密之组织数据
今天为大家带来的是数据库的基本用法 首先带大家了解下数据库. 大量的数据正在不断产生,伴随而来的是如何安全有效地存储.检索.管理他们. 对数据的有效存储.高效访问.方便共享和安全控制等问题成为信息 ...
- 联想 K5 Note(L38012)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI3.9.218
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- ionic2 打包时报错 file-opener2
在app自动更新过程中,有用到ionic-native插件:cordova-plugin-file-openner2 添加插件后,打包时有错: FAILURE: Build failed wit ...
- java与javascript之间json格式数据互转
javascript中对象与字符串的互转 对象转为字符串:通过JSON.encode方法,这个是json.js里面的方法,引入到当前文件就可以了. 字符串转换为对象:①使用JSON.decode方法, ...
- Python 之类型转换
# int(x[, base]) 将x转换为一个整数,base为进制,默认十进制 # # long(x[, base] ) 将x转换为一个长整数 # # float(x) 将x转换到一个浮点数 # # ...
- unittest 是什么?怎么用?
unittest单元测试框架详解 https://www.cnblogs.com/fighter007/p/8245063.html unittest最详细的解说 https://www.cnblog ...
- linux设置crontab定时执行脚本备份mysql
前言:mysqldump备份数据库命令 mysqldump -u root -psztx@2018 fengliuxiaosan > /dbbackup/fengliuxiaosan.sql## ...
- BZOJ 4520: [Cqoi2016]K远点对 KDtree + 估价函数 + 堆
Code: #include<bits/stdc++.h> #define ll long long #define maxn 200000 #define inf 10000000000 ...