在自己IntentSevice继承类中的onHandleIntent方法中写Toast.makeText(getApplicationContext(), "sd不存在", Toast.LENGTH_SHORT).show();时不会有任何提示,logcat中提示“sending message to a Handler on a dead thread”错误。后从网上差报错原因了解到当一个线程的消息循环已经退出后,不能再给其发送消息不如就会报错。后有从一个网站找到了解决方案,和产生错误的原因。

The problem here is that you are creating a Toast inside a thread that is managed by the IntentService. The system will use the Handler associated with this thread to show and hide the Toast.


First the Toast will be shown correctly, but when the system tries to hide it, after the onHandleIntent method has finished, the error "sending message to a Handler on a dead thread" will be thrown because the thread on which the Toast was created is no longer valid, and the Toast will not disappear.


这里的问题是,你正在创建一个由IntentService管理的Toast内部线程。该系统将使用与此线程关联的处理程序,以显示和隐藏Toast。首先,Toast将正确显示出来,但是当系统试图将其隐藏时onHandleIntent方法已经完成,“sending message to a Handler on a dead thread”的错误将被抛出,因为Toast在其上创建的线程不再有效,且Toast不会消失。

解决方法:

// create a handler to post messages to the main thread
    Handler mHandler = new Handler(getMainLooper());
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
        }
    });



引用:http://www.xuebuyuan.com/36739.html