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. lua_gc源码学习

    最近发现在大数据量的 lua 环境中,GC 占据了很多的 CPU .差不多是整个 CPU 时间的 20% 左右.希望着手改进.这样,必须先对 lua 的 gc 算法极其实现有一个详尽的理解.我之前读过 ...

  2. VMware 14 的永久许可密钥

    VMware workstation 14永久激活密钥分享: CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ZC3WK-AFXEK-488JP-A7MQX-XL8YF AC5XK-0ZD ...

  3. iOS - AVAudioSession详解

    音频输出作为硬件资源,对于iOS系统来说是唯一的,那么要如何协调和各个App之间对这个稀缺的硬件持有关系呢? iOS给出的解决方案是"AVAudioSession" ,通过它可以实 ...

  4. 【swoole2.0】 PHP + swoole2.0 初体验

    背景: centos7   PHP7.1   swoole2.0 准备工作: 一.  swoole  扩展安装 1 下载swoole cd /usr/local wget -c https://git ...

  5. USACO The Clocks

    操作间没有次序关系,同一个操作最多重复3次... 可以直接暴力... The Clocks IOI'94 - Day 2 Consider nine clocks arranged in a 3x3 ...

  6. delphi for android 获取手机号

    delphi for android 获取手机号 uses   System.SysUtils, System.Types, System.UITypes, System.Classes, Syste ...

  7. css3整理--text-overflow

    text-overflow语法: text-overflow : clip | ellipsis clip:表示不显示省略标记(...),而只是简单的裁切,需要在一定的高度范围内配合overflow: ...

  8. 路由器子网掩码设置不正确导致github无法访问

    奇怪的现象,路由器子网掩码设置成255.0.0.0会导致电脑访问不到https://github.com/ 改成默认的255.255.255.0就正常了.

  9. POJ 3273 Monthly Expense(二分答案)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...

  10. C语言位操作--奇偶校验算法

    信息是以比特流的方式传输的,类似01000001.在传输过程中,有可能会发生错误,比如,我们存储了01000001,但是取出来却是01000000,即低位由0变成了1.为了检测到这种错误,我们可以通过 ...