Android_handler (b)

Source: Internet
Author: User

Tag: Send message file message take Out star thread TCO pop effect

This article records the Android message mechanism. Workthread simulates a step in which a message is sent to Mainthread and changes the text of TextView after the data is obtained from the network. (Mainthread←workthread).

Steps:
1. Create a handler object in Mainthread. The replication Handlemessage () method.
2. Workthread calls the SendMessage () method of Handermessage () to send a message to the message queue
3, Looper in the message queue to remove the message object, to Mainthread handlemessage () method processing.

Code:

Layout

<textview         android:id= "@+id/textviewid"        android:layout_width= "match_parent"        android:layout_ height= "Wrap_content"        android:text= "Data"/>        <button        android:id= "@+id/buttonid"        android: Layout_width= "Match_parent"        android:layout_height= "wrap_content"        android:layout_below= "@id/textviewid"        android:text= "Send Message"/>
activity:
Package Com.away.b_07_handler02;import Android.app.activity;import Android.os.bundle;import Android.os.Handler; Import Android.os.message;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;public class Mainactivity extends Activity {private TextView Textview;private Button button;private Handler Handler; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TextView = (TextView) Findviewbyid ( R.ID.TEXTVIEWID); button = (button) Findviewbyid (r.id.buttonid); handler = new MyHandler (); Button.setonclicklistener ( New Buttonlistener ());} Class MyHandler extends Handler {@Overridepublic void Handlemessage (Message msg) {System.out.println ("handlermessage-- -->>: "+ thread.currentthread (). GetName ()); string s = (string) msg.obj;textview.settext (s);}} Class Buttonlistener implements Onclicklistener {@Overridepublic void OnClick (View arg0) {threAd t = new Networkthread (); T.start ();}} Class Networkthread extends Thread {@Overridepublic void run () {System.out.println ("network---->>:" + Thread.curr Entthread (). GetName ());//analog access to the network, so that when the thread sleeps, first sleep for 2 seconds try {thread.sleep (2 *);} catch (Interruptedexception e) { E.printstacktrace ();} The value of the variable s, which simulates the data obtained from the network as a string s = "Data obtained from the network";//Textview.settext (s); Such a practice is wrong, due to the fact that in Android systems, there are only mainthread which have the ability to operate uimessage msg = Handler.obtainmessage (); msg.obj = s;//sendMessage () method, Whether it is sent in the main thread or workers thread, it is capable of handler.sendmessage (msg);}}

Effect:

Welcome to AC http://blog.csdn.net/ycwol/article/details/42066449

Android_handler (b)

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.