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. (原)一句mpAudioPolicy->get_input引发的血案

    今天分析Android的Audio系统时,对mpAudioPolicy->get_input进行了分析,没想到这一句话的背后如此复杂,简直是一句话引出的血案啊! 分析结果如下:(关于排版:各个变 ...

  2. Linux用户态程序计时方式详解

    前言 良好的计时器可帮助程序开发人员确定程序的性能瓶颈,或对不同算法进行性能比较.但要精确测量程序的运行时间并不容易,因为进程切换.中断.共享的多用户.网络流量.高速缓存访问及转移预测等因素都会对程序 ...

  3. 【Phalapi】新加Namespace (模块)

    官网地址: https://www.phalapi.net/ github 地址: https://github.com/phalapi/phalapi/tree/master-2x 1 compos ...

  4. 【cs229-Lecture3】Logistic回归

    参考: http://www.itongji.cn/article/12112cH013.html http://blog.csdn.net/zouxy09/article/details/20319 ...

  5. python框架---->BeautifulSoup的使用

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.一个人至少拥有一个梦想,有一个理由去坚强.心 ...

  6. 【SpringBoot整合Elasticsearch】SpringBoot整合ElasticSearch

    一.Linux下安装ElasticSearch 1.检测是否安装了Elasticsearch ps aux |grep elasticsearch 2.安装JDK 3.下载Elasticsearch ...

  7. 如何修改 VIM 制表符的空格数?

     想修改一下编辑器vi里的制表符(Tab)的空格数.因为它默认的太长(默认是8个空格).  在网上搜到了这篇文章http://my.oschina.net/captaintheron/blog/515 ...

  8. Promise 必知必会的面试题

    Promise 想必大家都十分熟悉,想想就那么几个 api,可是你真的了解 Promise 吗?本文根据 Promise 的一些知识点总结了十道题,看看你能做对几道. 以下 promise 均指代 P ...

  9. Elasticsearch学习之深入搜索一 --- 提高查询的精准度

    1. 为帖子增加标题字段 POST /forum/article/_bulk { "} } { "doc" : {"title" : "th ...

  10. This dependency was not found: * !!vue-style-loader!css-loader?……解决方案

    1.新建一个vue项目时,需要重新安装stylus 报错 This dependency was not found: * !!vue-style-loader!css-loader?{"m ...