Android advanced tutorial (9)-How to Use Android handler !!!

Source: Internet
Author: User
Hello everyone, this section is about the use of Android handler. before talking about handler, let's raise a small question: how can the program update the title in five seconds. first, let's take a look at the Java programmers who are used to writing programs before they know how to use handler. The Code is as follows:
  1. Package com. Android. tutor;
  2. Import java. util. timer;
  3. Import java. util. timertask;
  4. Import Android. App. activity;
  5. Import Android. OS. Bundle;
  6. Public class handlerdemoextends activity {
  7. // Title provides variables for the settitle method. I have set this variable to the int type for convenience.
  8. Private int title = 0;
  9. Public void oncreate (bundle savedinstancestate ){
  10. Super. oncreate (savedinstancestate );
  11. Setcontentview (R. layout. Main );
  12. Timer timer = new timer ();
  13. Timer. scheduleatfixedrate (New mytask (), 1,5000 );
  14. }
  15. Private class mytaskextends timertask {
  16. @ Override
  17. Public void run (){
  18. Settitle ("welcome to Mr Wei's blog" + title );
  19. Title ++;
  20. }
  21. }
  22. }

Package COM. android. tutor; import Java. util. timer; import Java. util. timertask; import android. app. activity; import android. OS. bundle; public class handlerdemo extends activity {// Title provides variables for the settitle method. For convenience, I have set the int type private int title = 0; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); timer = new timer (); timer. scheduleatfixedrate (New mytask (), 1, 5000);} private class mytask extends timertask {@ overridepublic void run () {settitle ("welcome to Mr Wei's blog" + title); Title ++ ;}}}However, when we execute a program, we cannot achieve the expected results. Therefore, Android introduces the special class handler, which can be said to be a bridge between runnable and activity, therefore, we only need to send a message in the run method, and execute different tasks through different messages in handler. The modified code is as follows:

  1. Package com. Android. tutor;
  2. Import java. util. timer;
  3. Import java. util. timertask;
  4. Import Android. App. activity;
  5. Import Android. OS. Bundle;
  6. Import Android. OS. Handler;
  7. Import Android. OS. message;
  8. Public class handlerdemoextends activity {
  9. // Title provides variables for the settitle method. I have set this variable to the int type for convenience.
  10. Private int title = 0;
  11. Private handler mhandler = new handler (){
  12. Public void handlemessage (Message MSG ){
  13. Switch (msg. What ){
  14. Case 1:
  15. Updatetitle ();
  16. Break;
  17. }
  18. };
  19. };
  20. Public void oncreate (bundle savedinstancestate ){
  21. Super. oncreate (savedinstancestate );
  22. Setcontentview (R. layout. Main );
  23. Timer timer = new timer ();
  24. Timer. scheduleatfixedrate (New mytask (), 1,5000 );
  25. }
  26. Private class mytaskextends timertask {
  27. @ Override
  28. Public void run (){
  29. Message message = new message ();
  30. Message. What = 1;
  31. Mhandler. sendmessage (Message );
  32. }
  33. }
  34. Public void updatetitle (){
  35. Settitle ("welcome to Mr Wei's blog" + title );
  36. Title ++;
  37. }
  38. }
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.