W/MessageQueue: Handler (android.os.Handler) {4241f8f8} sending message to a Handler on a dead thread
稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread。
在另一次在IntentService里使用MediaPlayer 播放铃声也再现错误,信息是:Handler) {42414500} sending message to a Handler on a dead thread。
本次的完整信息是:

- W/ActivityManager( 1394): getTasks: caller 10034 is using old GET_TASKS but privileged; allowing
- W/MessageQueue( 7666): Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread
- W/MessageQueue( 7666): java.lang.IllegalStateException: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread
- W/MessageQueue( 7666): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325)
- W/MessageQueue( 7666): at android.os.Handler.enqueueMessage(Handler.java:635)
- W/MessageQueue( 7666): at android.os.Handler.sendMessageAtTime(Handler.java:604)
- W/MessageQueue( 7666): at android.os.Handler.sendMessageDelayed(Handler.java:574)
- W/MessageQueue( 7666): at android.os.Handler.postDelayed(Handler.java:398)
- W/MessageQueue( 7666): at com.bandwidthx.library.l.a(SourceFile:367)
- W/MessageQueue( 7666): at com.bandwidthx.library.l.B(SourceFile:357)
- W/MessageQueue( 7666): at com.bandwidthx.library.BxApproval.aZ(SourceFile:4563)
- W/MessageQueue( 7666): at com.bandwidthx.library.BxApproval.aT(SourceFile:4440)
- W/MessageQueue( 7666): at com.bandwidthx.library.BxApproval.aS(SourceFile:4431)
- W/MessageQueue( 7666): at com.bandwidthx.library.BxApproval.aG(SourceFile:4044)
- W/MessageQueue( 7666): at com.bandwidthx.library.l.a(SourceFile:1320)
- W/MessageQueue( 7666): at com.bandwidthx.library.l.j(SourceFile:1275)
- W/MessageQueue( 7666): at com.bandwidthx.library.q.w(SourceFile:2280)
- W/MessageQueue( 7666): at com.bandwidthx.library.q.a(SourceFile:3399)
- W/MessageQueue( 7666): at com.bandwidthx.library.q.a(SourceFile:3103)
- W/MessageQueue( 7666): at com.bandwidthx.library.q$1.a(SourceFile:1959)
- W/MessageQueue( 7666): at com.bandwidthx.library.q$1.doInBackground(SourceFile:1928)
- W/MessageQueue( 7666): at android.os.AsyncTask$2.call(AsyncTask.java:292)
- W/MessageQueue( 7666): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
- W/MessageQueue( 7666): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
- W/MessageQueue( 7666): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
- W/MessageQueue( 7666): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
- W/MessageQueue( 7666): at java.lang.Thread.run(Thread.java:818)

估计Looper啊,MessageQueue之类的,已经被你们说烂了。
我就简单概括一句(当然下面也有很详细的)
一旦一个线程的消息循环退出后,不能再给其发送消息,否则会有RuntimeException抛出。
就是你一般性看到的:
- "RuntimeException: Handler{xxxx} sending message to a Handler on a dead thread"。
一般性的,如果是你实现自己的Looper和Handler,建议在Looper.prepare()后,调用Looper.myLooper()来获取对这个线程Looper的引用。
用途:0. 可以调用quit()终止服务线程 1. 接收消息时检查消息循环是否已经退出
值得一说的是: 线程终止了,有时候并不是你自己终止的,很可能能是系统的某个正常的时序导致的(只是你没有注意到这个次序,然后写代码的时候没有注意)
上面已经说清楚了,下面是详细信息&啰嗦分界线(下面再多说也就这么回事儿,别往下看了,下面就说一个简单的案例)
案例:(使用 IntentService 的发送短信,代码如下)
在IntentService内部实际是开启的一个工作线程。

- @Override
- protected void onHandleIntent(Intent intent) {
- Bundle data = intent.getExtras();
- String[] recipients = null;
- String message = getString(R.string.unknown_event);
- String name = getString(R.string.app_name);
- if (data != null && data.containsKey(Constants.Services.RECIPIENTS)) {
- recipients = data.getStringArray(Constants.Services.RECIPIENTS);
- name = data.getString(Constants.Services.NAME);
- message = data.getString(Constants.Services.MESSAGE);
- for (int i = 0; i < recipients.length; i++) {
- if(!StringUtils.isNullOrEmpty(recipients[i])) {
- try {
- Intent sendIntent = new Intent(this, SMSReceiver.class);
- sendIntent.setAction(Constants.SMS.SEND_ACTION);
- PendingIntent sendPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- Intent deliveryIntent = new Intent(this, SMSReceiver.class);
- deliveryIntent.setAction(Constants.SMS.DELIVERED_ACTION);
- PendingIntent deliveryPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, deliveryIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- SmsManager.getDefault().sendTextMessage(recipients[i].trim(), null, "[" + name + "] " + message, sendPendingIntent, deliveryPendingIntent);
- } catch (Exception e) {
- Log.e(TAG, "sendTextMessage", e);
- e.printStackTrace();
- Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
- MainActivity.instance.writeToLogFile(e.getMessage(), System.currentTimeMillis());
- }
- }
- }
- }
- }

(明眼人一看就知道,那个catch语句写的有问题)
- catch (Exception e) {
- Log.e(TAG, "sendTextMessage", e);
- e.printStackTrace();
- Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
- MainActivity.instance.writeToLogFile(e.getMessage(), System.currentTimeMillis());
- }
结果就报错了:
Google了一下资料,网址上是这么说的:
(大意翻译: 原因是你在一个由intentService控制生命周期的线程种创建了toast,手机先会显示这个toast,之后还是用这个线程的handler去隐藏&关闭这个toast;但是当onHandleIntent结束的时候,这个线程就挂了,然后绑定在这个线程上的handler也就不存在了,完事儿这个toast就关闭不了了,怎么办呢?发给主线程吧,它的toast的Handeler还存活,可以隐藏toast)
解决方案呢?(简单,粗暴)
哪些问题还有什么没有说?
为什么会向一个死线程发消息呢?
因为onHandleIntent(Intent intent)结束了这个线程也就没有了,到底怎么回事儿?
handler没有了,所以cannot hide toast?
如果我没有记错的话,这个toast的生命周期应该是NotificationManagerService控制的吧?
(IntentService源码剖析)----TODO
(Toast生命周期源码剖析)----TODO
转自:http://www.cnblogs.com/longhs/p/5557711.html
W/MessageQueue: Handler (android.os.Handler) {4241f8f8} sending message to a Handler on a dead thread的更多相关文章
- 缩略信息是: sending message to a Handler on a dead thread 我是用IntentService时报的
稍微纤细一点儿的信息是: Handler (android.os.Handler) {215ddea8} sending message to a Handler on a dead thread. ...
- Android全面解析之由浅及深Handler消息机制
前言 很高兴遇见你~ 欢迎阅读我的文章. 关于Handler的博客可谓是俯拾皆是,而这也是一个老生常谈的话题,可见的他非常基础,也非常重要.但很多的博客,却很少有从入门开始介绍,这在我一开始学习的时候 ...
- java.lang.RuntimeException: Handler (com.***.behavior.BEvent$1) {421bca80} sending message to a Hand
java.lang.RuntimeException: Handler (com.***.behavior.BEvent$1) {421bca80} sending message to a Hand ...
- 解决发http get请求的时候不成功,出现android.os.NetworkOnMainThreadException的异常
问题描述:在接游戏sdk的时候,由于游戏要求购买的时候是在主线程里面进行的,但是发http请求是不能在主线程里面发,否则就会出现android.os.NetworkOnMainThreadExcept ...
- Caused by: android.os.TransactionTooLargeException总结
错误信息 Error: android.os.TransactionTooLargeException W/ActivityManager(344): android.os.TransactionTo ...
- Android菜鸟的成长笔记(12)——Handler、Loop、MessageQueue
原文:[置顶] Android菜鸟的成长笔记(12)——Handler.Loop.MessageQueue 当一个程序第一次启动时,Android会启动一条主线程(Main Thread),主线程主要 ...
- Android Handler机制 (一个Thead中可以建立多个Hander,通过msg.target保证MessageQueue中的每个msg交由发送message的handler进行处理 ,但是 每个线程中最多只有一个Looper,肯定也就一个MessageQuque)
转载自http://blog.csdn.net/stonecao/article/details/6417364 在android中提供了一种异步回调机制Handler,使用它,我们可以在完成一个很长 ...
- android.os.Handler
android.os.handler A Handler allows you to send and process Message and Runnable objects associated ...
- Android多线程源码学习笔记一:handler、looper、message、messageQueue
最近在学习Android多线程相关知识的源码,现在把自己的笔记整理一下,写出来加深印象. Android多线程通讯的核心是handler.looper.message.messageQueue,这篇文 ...
随机推荐
- xcode 在哪里新建category、protocol等文件
1.和以前新建新文件一样.2.当然选IOS啦,不过OS X也有这个选项,然后Objctive-C File. 3.在File Type里选就OK啦.
- Linux内核(11) - 子系统的初始化之内核选项解析
首先感谢国家.其次感谢上大的钟莉颖,让我知道了大学不仅有校花,还有校鸡,而且很多时候这两者其实没什么差别.最后感谢清华女刘静,让我深刻体会到了素质教育的重要性,让我感到有责任写写子系统的初始化. 各个 ...
- Python 列表 remove() 方法
描述 Python 列表 remove() 方法通过指定元素的值来移除列表中某个元素的第一个匹配项,如果这个元素不在列表中会报一个异常. 语法 remove() 方法语法: L.remove(obj) ...
- 从jar包中读取资源
package myspider; import java.io.UnsupportedEncodingException; /** * * @author mark */ public class ...
- 使用python执行linux命令
python版本是2.7.12 一.简单的获取linux命令的执行结果,比如:获取一个PID的进程树结构,linux命令是pstree -p pid,在python中有一个模块可以方便的获取.至于有时 ...
- 读书笔记6pandas简单使用
一.序列Series,很像numpy中的array数组,可以由列表.元组.字典.numpy中的array来初始化 >>> from pandas import Series > ...
- cocos2dx 3.3 getParentToNodeTransform bug
cocos2dx 3.3中getParentToNodeTransform实现如下: const Mat4& Node::getParentToNodeTransform() const { ...
- JavaScript:表单常用验证脚本(整理)
以下内容根据网上资源整理而来,主要来源是CSDN一个供下载的check.js,源码地址找不到了. 1. 检查输入字符串是否为空或者全部都是空格 /* 检查输入字符串是否为空或者全部都是空格 输入:st ...
- 修改easyui的easyloader的默认css目录路径
easyloader默认情况下会使用js文件所在目录下的themes文件夹中的css,这里改成项目自定义的css文件夹. 首先找到: var m=src.match(/easyloader\.js(\ ...
- vim:查看当前的配置文件名称和地址
vim把当前使用的配置文件地址放置在变量MYVIMRC中.在vim使用echo查看即可: :echo $MYVIMRC