Android(八) HandlerThread
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的更多相关文章
- Android 开发 HandlerThread详解 转载
转载请注明出处:http://blog.csdn.net/vnanyesheshou/article/details/75073307 对于Handler不太懂的可以参考我的这两篇文章: Androi ...
- Android之HandlerThread
HandlerThread详解 1 HandlerThread基本原理 HandlerThread继承自Thread,它是一种可以使用Handler的Thread.它的实现很简单,就是在run方法中通 ...
- Android 八款开源 Android 游戏引擎
原文地址 本文内容 Angle Rokon LGame AndEngine libgdx jPCT Alien3d Catcake 最近无意间看到一篇关于 Android 搜索引擎的文章,于是搜索了, ...
- Android开发——HandlerThread以及IntentService详解
.HandlerThread Android API提供了HandlerThread来创建线程.官网的解释是: //Handy class for starting a new thread that ...
- Android中HandlerThread的使用及源代码解析
关于Hanlder的基本使用能够參见博文<Android中Handler的使用>,假设想了解Handler.Looper.Thread等的相互关系以及内部实现原理能够參见博文<深入源 ...
- Android多线程—HandlerThread解析
一.HandlerThread作用 1.实现多线程:在工作线程之后执行任务(比如一些耗时任务) 2.异步通信.消息传递:实现工作线程与主线程(UI线程)之间的通信,即将工作线程的执行结果传递给主线程, ...
- Android HandlerThread 总结使用
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6062880.html 本文出自[赵彦军的博客] 前言 以前我在 [Android Handler.Loop ...
- Android HandlerThread 的使用及其Demo (转)
转自http://www.cnblogs.com/hnrainll/p/3597246.html 介绍 首先我们来看看为什么我们要使用HandlerThread?在我们的应用程序当中为了实现同时完成多 ...
- 【Android】用HandlerThread模拟AsyncTask功能(ThreadTask)
前言 AsyncTask是个好东西,能处理绝大多数应用线程和更新UI的任务,由于其内部使用了静态线程池,如果你有一堆异步任务(例如全局定时更新数据.同一个Activity中多个AsyncTask同时执 ...
随机推荐
- Linux中的邮件发送
这里写出两种常用的邮件发送方式: mail: 需要安装sendmail和postfix两个服务 编辑/etc/mail.rc,在最后添加 set from=scottcho@126.com smtp= ...
- code_blocks 使用操作手册
38 39 编译以上程序,产生如下提示信息. 如此简 ...
- (原创)使用mceusb设备,将lirc移植到android笔记
首先说一下大环境和总体步骤: 下载lirc 0.8.7源码,使用ubuntu的setup.sh,配置为mceusb的驱动,同时Compile tools for X-Windows选项去掉,生成con ...
- apache使某目录下的文件能够列表显示出来
想要使web目录下,某目录下的文件列表显示而不是显示"You don't have permission to access / on this server" 需要在httpd. ...
- 使用VS Code写PHP并进行调试
VS Code(Visual Studio Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器. 1.先从官网下载安装好VS Code.官方下载地址是https://code.visua ...
- git怎么使用
1_创建一个git服务器 2_开发人员小A从服务器拉取代码 3_小A提交代码 4_小c拉取代码 5_小a现在的代码 6_小c改变了小a的代码 7_小c将变更提交一下 8_小a拉取服务器的代码 9_小A ...
- android:listView Button 焦点问题
要想listView的item与其上的button皆能得到焦点响应: 在listView item 的布局中: 在<RelativeLayout>中 android:descendantF ...
- 【大数据系列】hadoop脚本分析
一.start-all.sh hadoop安装目录/home/hadoop/hadoop-2.8.0/ libexec/hadoop-config.sh ---设置变量 sbin/start- ...
- [BeiJing2011]元素[贪心+线性基]
2460: [BeiJing2011]元素 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1245 Solved: 652[Submit][Stat ...
- 关于servlet3.0中的异步servlet
刚看了一下维基百科上的介绍,servlet3.0是2009年随着JavaEE6.0发布的: 到现在已经有六七年的时间了,在我第一次接触java的时候(2011年),servlet3.0就已经出现很久了 ...