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报 ...
随机推荐
- Linq学习(零)-错误汇总
问题一: Cannot execute text selection: CS0009 Metadata file 'C:\Users\Kimisme\Documents\LINQPad Plugins ...
- 【年终糖果计划】跟风领一波糖果 candy.one 领取教程
糖果领取网址(较为稳定):https://candy.one/i/1474564 用微信和QQ打开的朋友请复制到其他浏览器打开 糖果领取网址(较为稳定):https://candy.one/i/147 ...
- Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事
Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...
- [Android]有关外部链接唤醒App需要注意的坑
移动互联网发展到今天,一个移动app需要和各种各样的外部链接关联,它不再仅仅从手机的桌面启动,更多的将会从其他的应用.浏览器链接.短信.二维码或者微信分享等渠道启动,这里涉及到的是各种各样的营销渠道和 ...
- BeginEditorCommand()
BeginEditorCommand();开始把焦点给CAD CompleteEditorCommand();焦点给窗体
- Windows系统设置与北京Internet时间同步
找到提供Ntp服务器的网址:http://support.ntp.org/我从中找到东北大学的: ntp.neu.edu.cn ( 202.118.1.46 ) 长话短说,下面开始设置: 修改后的值 ...
- hdu 4018 Parsing URL(字符串截取)
题目 以下引用自百度百科: sscanf 的相关用法 头文件:#include<stdio.h> 1. 常见用法. 1 2 3 charbuf[512]; sscanf(" ...
- 刽子手游戏(Hangman Judge, UVa 489)
刽子手游戏其实是一款猜单词游戏,游戏规则是这样的:计算机想一个单词让你猜,你每次可以猜一个字母.如果单词里有那个字母,所有该字母会显示出来:如果没有那个字母,则计算机会在一幅“刽子手”画上填一笔.这幅 ...
- 从CSV文件中读取jpg图片的URL地址并多线程批量下载
很多时候,我们的网站上传图片时并没有根据内容进行文件夹分类,甚至会直接存储到阿里云的OSS或是七牛云等云存储上.这样,当我们需要打包图片时,就需要从数据库找寻分类图片,通过CURL进行下载.我最近刚刚 ...
- 使用PHP操作MongoDB数据库
1.连接MongoDB数据库(在已安装php-mongodb扩展的前提下) $config = "mongodb://{$user}:{$pass}@{$host}:{$port}" ...