Android main Thread and sub-Thread communication (Thread + handler)

Source: Internet
Author: User

Android main Thread and sub-Thread communication (Thread + handler)
Android is based on Java, so it is also divided into main threads and subthreads!
Main thread: implements business logic, UI rendering and updating, and connects sub-threads, similar to generals;
Sub-thread: completes time-consuming operations (online data retrieval, SD card data loading, and long-running background), similar to Xiao Bing;

I. subthreads send messages to the main Thread (Thread + handler ):
1. Handler defined in the main thread:
Java code

  1. Handler mHandler = new Handler (){
  2.  
  3. @ Override
  4. Public void handleMessage (Message msg ){
  5. Super. handleMessage (msg );
  6. Switch (msg. what ){
  7. Case 0:
  8. // Do something, refresh UI;
  9. Break;
  10. Default:
  11. Break;
  12. }
  13. }
  14.  
  15. };

    2. The subthread sends a message to the main thread after processing the time-consuming operation and updates the UI:
    Java code
    1. MHandler. sendEmptyMessage (0 );
      In this way, the message interaction is completed under the condition that the subthread and the main thread have a task division;

      2. The main Thread sends messages to the subthread (Thread + handler ):
      When the main thread encounters a time-consuming operation, it must be completed by the subthread, and a notification is sent to the subthread. The procedure is as follows:
      1. Define Handler in the Child thread. The Handler defines the thread in which it is bound, and the Handler in the thread needs to call logoff. prepare (); method. It is not called in the main thread because the main thread calls it for you by default;
      Java code
      1. Public class LoopThread implements Runnable {
      2.  
      3. Public Handler mHandler = null;
      4.  
      5. @ Override
      6. Public void run (){
      7. Logoff. prepare ();
      8. MHandler = new Handler (){
      9. Public void handleMessage (Message msg ){
      10. String result = NetUtil. getJsonContent ("Beijing ");
      11. // Completed the weather retrieval operation in Beijing;
      12. Log. I ("test", "handler" + result );
      13. }
      14. };
      15. Logoff. loop ();
      16. }
      17.  
      18. }
        Among them, lorule. prepare (); and lorule. loop (); maintain a message queue, waiting for message injection and execution in the Child thread;
        2. This is called in the main thread:
        Java code
        1. LThread. mHandler. sendEmptyMessage (0 );

          The main thread sends a message to the subthread so that the subthread executes the specified operation. In Android, another method is HandlerThread. See the following example:
          Java code
          1. HandlerThread handlerThread = new HandlerThread ("jerome ");
          2. HandlerThread. start ();
          3.  
          4. /**
          5. * The logoff created by HandlerThread must be passed to threadHandler to complete the binding;
          6. */
          7. ThreadHandler = new Handler (handlerThread. getLooper ()){
          8.  
          9. @ Override
          10. Public void handleMessage (Message msg ){
          11. Super. handleMessage (msg );
          12. Switch (msg. what ){
          13. Case 0:
          14. Time-consuming operations can be performed here;
          15. Log. I ("jerome", "hello, I am sub thread ");
          16. Break;
          17. Default:
          18. Break;
          19. }
          20. }
          21. };

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.