Android main thread and child thread communication issues

Source: Internet
Author: User


Android does not now support view creation and invocation of its methods in child threads. If you want to implement the sub-thread content update, feedback the results to the main thread in a timely manner, how to come out?

You can create handler in the main thread to implement it. As a result of this thread, you can notify the main thread by sending a message, and then update the view control in the main thread in a timely way.


How to use handler:

Mhandler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Switch (msg.what) {
Case XXXXXX:
Updatelotteryinfo ();
Break

Default
Break
}
}
};


When you send a message, you can: Mhandler.sendemptymessage (xxxxx);

If you add content, you can: Mhandler.obtainmessage (MessageName, messagecontent). Sendtotarget ();

In method Handlemessage, you can get the contents of the message msg.obj.toString ().

There are other ways to send a message and you can check it yourself.

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.