1.Looper

  Looper used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create

one, call prepare() in the thread that is to run the loop, and then loop() to have it process messages until the loop is stopped.

2.HandlerThread 为我们解决问题

  Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start()

must still be called.

public class HandlerThread extends Thread {

    Looper mLooper;

    protected void onLooperPrepared() {
} public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();            //为mLooper赋值,唤醒等待的线程
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
} public Looper getLooper() {
if (!isAlive()) { //线程是否启动
return null;
} synchronized (this) {
while (isAlive() && mLooper == null) {
try {
wait(); // 如果mLooper为null则等待
} catch (InterruptedException e) {
}
}
}
return mLooper;
}
}

3.使用HandlerThread

      HandlerThread thread = new HandlerThread("handlerThread");
thread.start(); Looper looper = thread.getLooper();
Handler handler = new ServiceHandler(looper); //传入Looper参数,初始化Handler

Android(八) HandlerThread的更多相关文章

  1. Android 开发 HandlerThread详解 转载

    转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/75073307 对于Handler不太懂的可以参考我的这两篇文章: Androi ...

  2. Android之HandlerThread

    HandlerThread详解 1 HandlerThread基本原理 HandlerThread继承自Thread,它是一种可以使用Handler的Thread.它的实现很简单,就是在run方法中通 ...

  3. Android 八款开源 Android 游戏引擎

    原文地址 本文内容 Angle Rokon LGame AndEngine libgdx jPCT Alien3d Catcake 最近无意间看到一篇关于 Android 搜索引擎的文章,于是搜索了, ...

  4. Android开发——HandlerThread以及IntentService详解

    .HandlerThread Android API提供了HandlerThread来创建线程.官网的解释是: //Handy class for starting a new thread that ...

  5. Android中HandlerThread的使用及源代码解析

    关于Hanlder的基本使用能够參见博文<Android中Handler的使用>,假设想了解Handler.Looper.Thread等的相互关系以及内部实现原理能够參见博文<深入源 ...

  6. Android多线程—HandlerThread解析

    一.HandlerThread作用 1.实现多线程:在工作线程之后执行任务(比如一些耗时任务) 2.异步通信.消息传递:实现工作线程与主线程(UI线程)之间的通信,即将工作线程的执行结果传递给主线程, ...

  7. Android HandlerThread 总结使用

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6062880.html 本文出自[赵彦军的博客] 前言 以前我在 [Android Handler.Loop ...

  8. Android HandlerThread 的使用及其Demo (转)

    转自http://www.cnblogs.com/hnrainll/p/3597246.html 介绍 首先我们来看看为什么我们要使用HandlerThread?在我们的应用程序当中为了实现同时完成多 ...

  9. 【Android】用HandlerThread模拟AsyncTask功能(ThreadTask)

    前言 AsyncTask是个好东西,能处理绝大多数应用线程和更新UI的任务,由于其内部使用了静态线程池,如果你有一堆异步任务(例如全局定时更新数据.同一个Activity中多个AsyncTask同时执 ...

随机推荐

  1. [JS] console.time() - 计时器构造函数及如何计时

    概述 使用计时器可以对代码运行过程进行测速.你可以给每个计时器取一个名字,每个页面上最多可以运行一万个计时器.当你使用计时器名字调用 console.timeEnd() 函数时,浏览器会返回一个毫秒值 ...

  2. web基础----->jersey整合jetty开发restful应用(一)

    这里介绍一个jersey与jetty整合开发restful应用的知识.将过去和羁绊全部丢弃,不要吝惜那为了梦想流下的泪水. jersey与jetty的整合 一.创建一个maven项目,pom.xml的 ...

  3. IPhoneX网页布局 全屏布局(转)

    IPhoneX全面屏是十分科技化的,但是由于其圆角和摄像头刘海位置以及操控黑条的存在使得我们需要去对其样式做一些适配,没有X的同学可以开启 Xcode 9 的iPhone X 模拟器作为学习和调试. ...

  4. Python 2.7.6 安装lxml模块[ubuntu14.04 LTS]

    lxml --->首字母是字母l,不是数字1 lxml 2.x : https://pypi.python.org/pypi/lxml/2.3 1xml官网:http://lxml.de/ 一 ...

  5. Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MariaDB (instead of MySQL)

    https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ This tut ...

  6. Linux "bring up eth0 failed, eth0 seems not be presernt" 问题解决方案

    =========1.问题========== 重启网卡的时候出现"bring up eth0 failed, eth0 seems not be presernt", 提示找不到 ...

  7. centos 6.6编译安装nginx

    nginx 安装 安装前必要软件准备 1)安装pcre.gzip 等为了支持rewrite功能,我们需要安装pcre # yum install -y pcre* zlib zlib-devel op ...

  8. 大话FLASH和EEPROM

    最近在看代码的时候,遇到了一个使用FLASH模拟EEPROM的情况,看到这个我当时是一脸蒙蔽啊,对于一个有时候连FLASH和EEPROM都分不清的人来说,怎么可能读懂用FLASH来模拟EEPROM呢? ...

  9. sklearn的快速使用

    传统的机器学习任务从开始到建模的一般流程是:获取数据 -> 数据预处理 -> 训练建模 -> 模型评估 -> 预测,分类.本文我们将依据传统机器学习的流程,看看在每一步流程中都 ...

  10. mysql 小于等于0 不包含null

    SELECT count(*) FROM test2 WHERE num<=1;