Intentservice using Toask error sending message to a Handler on a dead thread

Source: Internet
Author: User

Write Toast.maketext (Getapplicationcontext (), "SD does not exist", toast.length_short) in the Onhandleintent method in your own Intentsevice inheritance class. Show ( ); When there is no prompt, logcat "Sending message to a Handler on a dead thread" error. After the error from the Internet to understand the reason that when a thread's message loop has exited, you can no longer send a message to the error. After having found a solution from a Web site, and the cause of the error.

The problem here is, that's creating a Toast inside a thread that's managed by the Intentservice. The system would use the Handler associated with this thread to show and hide the Toast.


First the Toast would be shown correctly, if the system tries to hide it, after the Onhandleintent method had finishe D, the error "sending message to a Handler on a dead thread" would be thrown because the thread on which the Toast was Crea Ted is no longer valid, and the Toast would not disappear.


The problem here is that you are creating a toast internal thread that is managed by Intentservice. The system will use the handler associated with this thread to show and hide the toast. First, the toast will be displayed correctly, but when the system tries to hide it, the Onhandleintent method is complete, and 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 does not disappear.

Workaround:

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 (); }    });



Reference: http://www.xuebuyuan.com/36739.html


Intentservice using Toask error sending message to a Handler on a dead thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.