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. Spring系列之IOC容器

    一.概述 IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象之间的依赖.应用程序无需直接在代码中new 相关的对象,应用程序由IOC容器进行组装.在S ...

  2. 【python3】爬取新浪的栏目分类

    目标地址: http://www.sina.com.cn/ 查看源代码,分析: 1 整个分类 在 div main-nav 里边包含 2 分组情况:1,4一组 . 2,3一组 . 5 一组 .6一组 ...

  3. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  4. vue笔记 - 生命周期第二次学习与理解

    对于刚接触vue一两个月.才仅仅独立做过一两个vue项目的小白来说,以前一直自我感觉自己知道vue的生命周期, 直到前两天去面试,面试官让我说一下vue的生命周期... 其实我的心中是有那张图的,但是 ...

  5. 如何将Ubuntu左边的面板放到底部

    直入主题,有些人不喜欢ubuntu默认的面板在左边(笔者就是~囧~),我还是喜欢将面板放入到桌面的底部,这样更符合自己的使用习惯,但是ubuntu默认是不支持的,需要通过配置工具来配置. 这个时候我们 ...

  6. Big Spatio temporal Data(R-tree Index and NN & RNN & Skyline)

    一.简单介绍大数据技术产物 “大数据”一词首先出现在2008年9月<Nature>杂志发表的一篇名为“Big Data: Wikiomics”的文章上(Mitch,2008).“大数据科学 ...

  7. 部署OpenStack问题汇总(五)--openstack中删除虚拟主机,状态一直未deleting

    [原创文章,转载请注明出处] 一.我重启了该机器,之后想删除没有创建成功的虚拟机(没有打开cpu的vt),结果发现状态一直为deleting状态.在这个状态下创建虚拟机也失败. 二.分析:在/var/ ...

  8. yii---判断POST请求

    我们在进行数据的提交的时候,很多时候会判断请求状态来进行不同的选择.常见的就是判断POST以及GET的请求方式,下面是YII判断POST请求的代码示例: public function actionP ...

  9. thinkCMF----公共模板的引入

    这个主要用于前台模板的 头部和底部分离: 具体引入方法: <include file="public@source"/> <include file=" ...

  10. 7.22 python面试题

    2018-7-22 16:32:24 把面试题敲完了,,好强悍! Python 10期考试题 1.常用字符串格式化有那些?并说明他们的区别 # format 直接调用函数 # %s 语法塘 # %r ...