http://hi.baidu.com/donghaozheng/item/dc843d149968bf6e70d5e8e4
(1) Looper类别用来为一个线程开启一个消息循环。默认情况下Android中新诞生的线程是没有开启消息循环的。(主线程除外,主线程系统会自动为其创建Looper对象,开启消息循环)
Looper对象通过MessageQueue来存放消息和事件。一个线程只能有一个Looper,对应一个MessageQueue。
(2) 通常是通过Handler对象来与Looper交互的。Handler可看做是Looper的一个接口,用来向指定的Looper发送消息及定义处理方法。
默认情况下Handler会与其被定义时所在线程的Looper绑定,比如,在主线程中定义,其是与主线程的Looper绑定。
mainHandler = new Handler() 等价于new Handler(Looper.myLooper()).
Looper.myLooper():Return the Looper object associated with the current thread 获取当前进程的looper对象。
还有一个类似的 Looper.getMainLooper() 用于获取主线程的Looper对象。
(3) 在非主线程中直接new Handler() 会报如下的错误:
E/AndroidRuntime( 6173): Uncaught handler: thread Thread-8 exiting due to uncaught exception
E/AndroidRuntime( 6173): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
原因是非主线程中默认没有创建Looper对象,需要先调用Looper.prepare()启用Looper。
(4) Looper.loop(); 让Looper开始工作,从消息队列里取消息,处理消息。
注意:写在Looper.loop()之后的代码不会被执行,这个函数内部应该是一个循环,当调用mHandler.getLooper().quit()后,loop才会中止,其后的代码才能得以运行。
(5) 基于以上知识,可实现主线程给子线程(非主线程)发送消息。
把下面例子中的mHandler声明成类成员,在主线程通过mHandler发送消息即可。
(6) Android官方文档中Looper的介绍:
Class 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 in the thread that is to run the loop, and then to have it process messages until the loop is stopped.
Most interaction with a message loop is through the class.
This is a typical example of the implementation of a Looper thread, using the separation of and to create an initial Handler to communicate with the Looper.
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
分享到:
相关推荐
解决 java.lang.RuntimeException: Could not generate DH keypair异常处理。 bcprov-ext-jdk15on-1.60、bcprov-jdk15on-1.60两个包放到jre下的$JAVA_HOME/jre/lib/ext的路径下,然后配置$JAVA_HOME/jre/lib/...
java.lang.RuntimeException: Unsupported algorithm: HmacSHA1 解决方法,阿里云
java.lang.runtimeexception unexpected subelement
主要介绍了javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair 解决方法,有需要的朋友们可以学习下。
报错 java.lang.OutOfMemoryError: PermGen space 报错 java.lang.OutOfMemoryError: Java heap 启动报错java.lang.ClassNotFoundException: 1catalina.org.apache.juli.FileHandler JAVA_OPTS="-server -Xms800m ...
基于dex2jar 2.0版本解决 java.lang.RuntimeException: can not merge I and Z问题
1、修改jdk/jre/lib/security/java.security,加粗部分放到最前面,其它依次修改编号放到后面 security.provider.1=org.bouncycastle.jce.provider.BouncyCastleProvider security.provider.2=sun.security....
java.lang.UnsupportedOperationException异常(csdn)————程序
NULL 博文链接:https://jameskaron.iteye.com/blog/2291495
加密是报:javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair 此处提供 bcprov-ext-jdk15on-154.jar 和 bcprov-jdk15on-154.jar 压缩包中有使用说明
1.基于 trie 树结构实现高效词图扫描 2.生成所有切词可能的有向无环图 DAG 3.采用动态规划算法计算最佳切词组合 4.基于 HMM 模型,采用 Viterbi (维特比)算法实现未登录词识别
java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(ImageIO.java:1388) at com.pleanwar.fiying.FlyingObject.loadImage...
NULL 博文链接:https://listen-raining.iteye.com/blog/1962065
2352): java.lang.RuntimeException: Unable to create service com.android.systemui.keyguard.KeyguardService: java.lang.SecurityException: read wallpaper: Neither user 10044 nor current process has ...
java.lang.RuntimeException java.lang.ArithmeticException 当出现异常的运算条件时,抛出此异常。 这个异常的解释是 "数学运算异常 ",比如程序中出现了除以零这样的运算就会出这样的异常,对这种异常,...
Android Caused by: java.lang.ClassNotFoundException解决办法 出现问题: 08-13 18:29:22.924: E/AndroidRuntime(1875): Caused by: Java.lang.ClassNotFoundException:XXXXX in loader dalvik.system....
下面小编就为大家带来一篇完美解决java.lang.OutOfMemoryError处理错误的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
开一个新线程,引起的异常。1. ANR: application not responding 2.android.view.ViewRoot$...3.java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
4)java.lang.RuntimeException: java.lang.RuntimeException: Hive Runtime Error while closing operators: null 5)hive on tez 最终insert的表如果使用到union all 时会导致直接查询结果表数据为空的
主要介绍了Exception in thread main java.lang.NoClassDefFoundError错误解决方法,需要的朋友可以参考下