2017.12.18 Android Development Message Queuing (implementing child threads modifying UI components)

Source: Internet
Author: User

1. Interface layout, and component initialization:

Component initialization:

Private button button;     Private Handler Handler;    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {        super.oncreate ( Savedinstancestate);        Setcontentview (r.layout.activity_main);        Button=(Button) Findviewbyid (R.id.button);        Button.setonclicklistener (New Buttonlistener ());        Handler=new   firsthandler ();    }

2. Use of Message Queuing:

    classButtonlistener Implements view.onclicklistener{//set up the listener so that each button generates one message at a time.@Override Public voidOnClick (view view) {Message msg=Handler.obtainmessage (); Msg.what=8;//MSG is one of the parameters that can be set for many msg parametersHandler.sendmessage (msg);//throw the news out .        }    }    classFirsthandler extends handler{@Override Public voidHandlemessage (Message msg) {//processing messages, passing a message body in, understood as an object, can be a string, type int, etc.           int what= msg.what; System. out. println ("data What is:"+what );        Super.handlemessage (msg); }    }

3. Modify the text of the main thread through Message Queuing:

 classMyHandler extends handler{@Override Public voidhandlemessage (Message msg) {System. out. println ("This is a message from MyHandler:"+Thread.CurrentThread (). GetName ()); String s=(String) msg.obj;        Textview.settext (s);    Super.handlemessage (msg); }}    classButtonlistener implements view.onclicklistener{@Override Public voidOnClick (view view) {thread thread=NewNetworkthread ();        Thread.Start (); }    }    classNetworkthread extends thread{@Override Public voidrun () {System. out. println ("information from Networkthread:"+Thread.CurrentThread (). GetName ()); Try{Thread.Sleep ( the); } Catch(interruptedexception e) {e.printstacktrace (); } String S="have the ideal patriotic youth"; Message msg=Handler.obtainmessage (); Msg.obj=s;            Handler.sendmessage (msg);        Super.run (); }    }

2017.12.18 Android Development Message Queuing (implementing child threads modifying UI components)

Related Article

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.