Android Threading and Handler

Source: Internet
Author: User

According to the video, I wrote a demo:

Package Com.wyl.wylthreadtest;import Android.graphics.color;import Android.os.bundle;import Android.os.Handler; Import Android.os.message;import Android.support.v4.app.fragment;import android.support.v7.app.ActionBarActivity; Import Android.view.layoutinflater;import android.view.menu;import android.view.menuitem;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.textview;public class Mainactivity extends actionbaractivity {TextView TextView; Button button;    Handler MyHandler;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        TextView = (TextView) Findviewbyid (R.ID.TEXTVIEWID);        Button = (button) Findviewbyid (R.id.buttonid);                MyHandler = new MyHandler ();        Buttonlistener listener = new Buttonlistener ();    Button.setonclicklistener (listener);  }    /**   * Add button's onclick listener * @author Administrator * */class Buttonlistener implements onclicklistener{@ Override public void OnClick (view view) {//Textview.settext ("My Goddess");//Textview.settextcolor (Color.gray);//S        Ystem.out.println ("Buttonlistener:" +thread.currentthread (). GetName ());    MyThread myThread = new MyThread ();    System.out.println ("Buttonlistener:" +thread.currentthread (). GetName ());    Mythread.start ();//Mythread.run ();  }}/** * Implements one of its own threads, simulates fetching data from the network and then passing it to handler * @author Administrator * */class MyThread extends        thread{String name = "Simulates fetching data from the network and then passing it to handler"; @Override public void Run () {try {sleep (1*1000)} catch (Interruptedexception e) {//TODO auto-generated catch bloc    Ke.printstacktrace ();}                System.out.println ("Mythead Child Thread" +thread.currentthread (). GetName ());    Message msg = Myhandler.obtainmessage ();    Msg.obj = name;    Myhandler.sendmessage (msg);   }            }         /** * Handler inherit Handler parent class * @author Administrator * */class MyHandler extends handler{@Ove    Rride public void Handlemessage (Message msg) {System.out.println ("MyHandler:" +thread.currentthread (). GetName ());    String name = (string) msg.obj;    Textview.settext (name); }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds I        TEMs to the Action Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }/** * A placEholder fragment containing a simple view. */public static class Placeholderfragment extends Fragment {public placeholderfragment () {} @Ov Erride public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancest            ATE) {View Rootview = inflater.inflate (R.layout.fragment_main, container, false);        return rootview; }    }}

As follows:

Android Threading and Handler

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.