Handler in Android

Source: Internet
Author: User

My example:

Package Com.wyl.wylhandler;import Android.os.bundle;import Android.os.handler;import android.os.message;import Android.support.v7.app.actionbaractivity;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.textview;public class Mainactivity extends Actionbaractivity { TextView TV; Button btn; Message msg;    Handler Handler;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TV = (TextView) Findviewbyid (R.ID.TEXTVIEWID);  Get TextView btn = (Button) Findviewbyid (R.id.buttonid);  Gets the button myonclicklistener L = new Myonclicklistener ();//Instantiate a listener L Btn.setonclicklistener (L); BTN bindings above gets the listener L} class Myonclicklistener implements onclicklistener{@Override public void OnClick (V   Iew v) {String c = "Wyl";//Set breakpoint with int len = C.length ();//Set Breakpoint with System.out.println ("Len:" +len); 1. Obtain the message MyHandler MyHandler = new MyHandler () through the handler object;    msg = Myhandler.obtainmessage ();    String a = "Zhang Ya lan";//Set breakpoint with msg.what = Len;    Msg.obj = A; Handler SendMessage (msg), Looper automatically removes MSG from the message queue (via the Handlemessage method), Myhandler.sendmessage (msg);//    Handler the MSG into the queue System.out.println ("onclick Thread:" +thread.currentthread (). GetName ()); }//Debug found that loop is in this line of time to get the value Handler in the queue msg} class MyHandler extends handler{@Override public void Handlemes SAGE (Message message) {String name = "Weiyongle";//Set breakpoint with int len2 = Name.length ();//Set Breakpoint with System.out.println ("    Len2: "+len2+", "+" MyHandler: "+thread.currentthread (). GetName ());    Object obj = message.obj;        System.out.println ("MyHandler" printed out: "+obj+" MyHandler the Thread: "+thread.currentthread (). GetName ()); }    }    }

  

  

Handler in Android

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.