Use handler to update the android UI

Source: Internet
Author: User

Original post link: http://rayleung.javaeye.com/blog/411860

 

When I first came into contact with Android Thread Programming, I used to be like Java and tried to solve the problem using the following code.

Java code
  1. New thread (New runnable (){
  2. Public void run (){
  3. Myview. invalidate ();
  4. }
  5. }). Start ();

 

However, this is not possible because it violates the single-threaded model: Android UI operations are not thread-safe and must be executed in the UI thread. After reading the document and apidemo, I found that the commonly used method is to use handler to update the UI thread.

 

The function of the following code is very simple: Draw a circle, every 0.1 seconds, the circle moves 10 pixels to 10. However, the process of updating the UI using handler is clearly displayed.

 

 

 

First, create a simple view. The Code is as follows:

Java code
  1. Package com. Ray. Handler;
  2. Import Android. content. context;
  3. Import Android. Graphics. Canvas;
  4. Import Android. Graphics. color;
  5. Import Android. Graphics. paint;
  6. Import Android. Graphics. Point;
  7. Import Android. Graphics. drawable. drawable;
  8. Import Android. View. view;
  9. Public class bounceview extends view {
  10. Float x = 40;
  11. Public bounceview (context ){
  12. Super (context );
  13. }
  14. @ Override
  15. Protected void ondraw (canvas ){
  16. X + = 10;
  17. Paint mpaint = new paint ();
  18. Mpaint. setantialias (true );
  19. Mpaint. setcolor (color. Green );
  20. Canvas. drawcircle (x, 40, 40, mpaint );
  21. }
  22. }

 

The code for creating an activity is as follows:

Java code
  1. Package com. Ray. Handler;
  2. Import Android. App. activity;
  3. Import Android. content. context;
  4. Import Android. Graphics. Canvas;
  5. Import Android. Graphics. color;
  6. Import Android. Graphics. paint;
  7. Import Android. OS. Bundle;
  8. Import Android. OS. Handler;
  9. Import Android. OS. message;
  10. Import Android. View. view;
  11. Import Android. View. window;
  12. Public class testhandler extends activity {
  13. Protected static final int guiupdateidentifier = 0x101;
  14. Thread myrefreshthread = NULL;
  15. Bounceview mybounceview = NULL;
  16. Handler myhandler = new handler (){
  17. Public void handlemessage (Message MSG ){
  18. Switch (msg. What ){
  19. Case testhandler. guiupdateidentifier:
  20. Mybounceview. invalidate ();
  21. Break;
  22. }
  23. Super. handlemessage (MSG );
  24. }
  25. };
  26. Public void oncreate (bundle savedinstancestate ){
  27. Super. oncreate (savedinstancestate );
  28. This. requestwindowfeature (window. feature_no_title );
  29. This. mybounceview = new bounceview (this );
  30. This. setcontentview (this. mybounceview );
  31. New thread (New mythread (). Start ();
  32. }
  33. Class mythread implements runnable {
  34. Public void run (){
  35. While (! Thread. currentthread (). isinterrupted ()){
  36. Message message = new message ();
  37. Message. What = testhandler. guiupdateidentifier;
  38. Testhandler. This. myhandler. sendmessage (Message );
  39. Try {
  40. Thread. Sleep (100 );
  41. } Catch (interruptedexception e ){
  42. Thread. currentthread (). Interrupt ();
  43. }
  44. }
  45. }
  46. }
  47. }
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.