Handler in Android

Source: Internet
Author: User

This article uses examples to verify and explain whether the thread enabled with the handler object is in the same thread as the main thread.

The program structure is as follows:

[1] The source code in handleractivity. Java is as follows:

[HTML]View plaincopyprint?
  1. Package com. andyidea. handlerdemo2;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. OS. Handler;
  5. Import Android. OS. handlerthread;
  6. Import Android. OS. logoff;
  7. Import Android. OS. message;
  8. Import Android. util. log;
  9. Public class handleractivity extends activity {
  10. Private handler = new handler ();
  11. @ Override
  12. Public void oncreate (bundle savedinstancestate ){
  13. Super. oncreate (savedinstancestate );
  14. // Handler. Post (R );
  15. Setcontentview (R. layout. Main );
  16. Log. E ("activity -->", thread. currentthread (). GETID () + "");
  17. Log. E ("activityname -->", thread. currentthread (). getname () + "");
  18. // Same thread as the main thread
  19. Handler. Post (R );
  20. // Different from the main thread
  21. Handlerthread = new handlerthread ("handler_thread ");
  22. Handlerthread. Start ();
  23. // After the handlerthread thread object is started,
  24. // You can use the handlerthread. getlooper () method to obtain the looper object.
  25. Myhandler = new myhandler (handlerthread. getlooper ());
  26. Message MSG = myhandler. obtainmessage ();
  27. MSG. sendtotarget (); // message sending Method
  28. }
  29. // Create a thread object
  30. Runnable r = new runnable (){
  31. @ Override
  32. Public void run (){
  33. Log. E ("Handler -->", thread. currentthread (). GETID () + "");
  34. Log. E ("handlername -->", thread. currentthread (). getname () + "");
  35. Try {
  36. Thread. Sleep (10*1000 );
  37. } Catch (interruptedexception e ){
  38. E. printstacktrace ();
  39. }
  40. }
  41. };
  42. // Create a handler object
  43. Class myhandler extends handler {
  44. Public myhandler (){
  45. }
  46. Public myhandler (low.logoff ){
  47. Super (logoff );
  48. }
  49. @ Override
  50. Public void handlemessage (Message MSG ){
  51. Log. E ("Handler -->", thread. currentthread (). GETID () + "");
  52. Log. E ("Handler -->", thread. currentthread (). getname () + "");
  53. }
  54. }
  55. }

Package COM. andyidea. handlerdemo2; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. OS. handlerthread; <br/> Import android. OS. logoff; <br/> Import android. OS. message; <br/> Import android. util. log; </P> <p> public class handleractivity extends activity {</P> <p> private handler = new handler (); </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // handler. post (r); <br/> setcontentview (R. layout. main); <br/> log. E ("activity -->", thread. currentthread (). GETID () + ""); <br/> log. E ("activityname -->", thread. currentthread (). getname () + ""); </P> <p> // same thread as the main thread <br/> handler. post (r); </P> <p> // different from the main thread <br/> handlerthread = new handlerthread ("handler_thread"); <br/> handlerthread. start (); <br/> // After the handlerthread thread object is to be started, <br/> // use handlerthread. the getlooper () method can obtain the looper object <br/> myhandler = new myhandler (handlerthread. getlooper (); <br/> message MSG = myhandler. obtainmessage (); <br/> MSG. sendtotarget (); // message sending method <br/>}</P> <p> // create a thread object <br/> runnable r = new runnable () {</P> <p> @ override <br/> Public void run () {<br/> log. E ("Handler -->", thread. currentthread (). GETID () + ""); <br/> log. E ("handlername -->", thread. currentthread (). getname () + ""); <br/> try {<br/> thread. sleep (10*1000); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> }; </P> <p> // create a handler object <br/> class myhandler extends handler {</P> <p> Public myhandler () {</P> <p >}</P> <p> Public myhandler (loggle) {<br/> super (loggle ); <br/>}</P> <p> @ override <br/> Public void handlemessage (Message MSG) {<br/> log. E ("Handler -->", thread. currentthread (). GETID () + ""); <br/> log. E ("Handler -->", thread. currentthread (). getname () + ""); <br/>}</P> <p >}< br/>}

After running the program, we can see the following results through the log. e output in the logcat console:

Through verification, we can see that if we only put a subthread object into the main thread queue through the post () method of the handler object, it is not a subthread is not enabled. The handlerthread object method enables a new thread to execute the program.

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.