Android Handler runnable use instance (key is stop in internal run)

Source: Internet
Author: User

. java

1  PackageCom.example.mydemo;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.Handler;6 ImportAndroid.os.Message;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.TextView; One  A  Public classMainactivityextendsActivity { -     PrivateTextView Text_view =NULL; -     PrivateButton start =NULL; the     PrivateButton end =NULL; -     Private intCount = 0; -     //The first thing to do when using handler is to create a handler -Handler Handler =NewHandler (); +     //to handle multithreading with handler, you can use the Runnable interface, where the interface is defined first -     //The run function that runs the interface in a thread +Runnable Update_thread =NewRunnable () { A          Public voidrun () { at             //the thread prints "Updatethread ..." text each time it executes and wraps it -             //TextView's append function is similar to the append in Qt and does not cover the front -             //content, but the append in Qt is automatically wrapped in the default mode . -             //text_view.append ("\nupdatethread ..."); -Text_view.settext (String.valueof (count++)); -             //delay 1s and add thread to thread queue in             if(Count >= 5) { -                 //handler.removecallbacks (update_thread);//stop with this line, you need to send a message outside to stop. Don't understand?  toMessage message =NewMessage (); +Message.what = 1; - handlerstop.sendmessage (message); the             } *Handler.postdelayed (Update_thread, 1000); $ Panax Notoginseng         } -     }; the  + @Override A      Public voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -  $Text_view =(TextView) Findviewbyid (R.id.text_view); $Start =(Button) Findviewbyid (R.id.start); -Start.setonclicklistener (NewStartclicklistener ()); -End =(Button) Findviewbyid (r.id.end); theEnd.setonclicklistener (NewEndclicklistener ()); - Handler.post (update_thread);Wuyi     } the  -     Private classStartclicklistenerImplementsOnclicklistener { Wu          Public voidOnClick (View v) { -             //TODO auto-generated Method Stub About             //to send the thread interface immediately to the thread queue $ Handler.post (update_thread); -         } -     } -  A     Private classEndclicklistenerImplementsOnclicklistener { +          Public voidOnClick (View v) { the             //TODO auto-generated Method Stub -             //to remove an interface from the thread queue $Count = 0; the handler.removecallbacks (update_thread); the         } the     } the  -     FinalHandler Handlerstop =NewHandler () { in          Public voidhandlemessage (Message msg) { the             Switch(msg.what) { the              Case1: AboutCount = 0; the handler.removecallbacks (update_thread); the                  Break; the             } +             Super. Handlemessage (msg); -         } the Bayi     }; the}

. xml

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical" >6 7<TextView8Android:id= "@+id/text_view"9Android:layout_width= "Fill_parent"Tenandroid:layout_height= "200dip" Oneandroid:text= "@string/hello_world" ATools:context= ". Mainactivity "/> -  -<Button theAndroid:id= "@+id/start" -Android:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" -android:text= "Start"/> +  -<Button +Android:id= "@+id/end" AAndroid:layout_width= "Fill_parent" atandroid:layout_height= "Wrap_content" -android:text= "End"/> -  -</LinearLayout>

Android Handler runnable use instance (key is stop in internal run)

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.