Android Thread Message Looper Handler Example

Android’s message queue and queue looper are aimed at the specific thread, a thread can have it’s own message queue and queue looper.

1. Android Message Queue And Looper Introduction.

  1. If you want to send messages between different threads, you need to add a message object in that thread’s message queue. Then the queue looper will fetch the message and process it.
  2. In android development, Activity is commonly used as the main thread. Android OS will create a message queue and queue looper for the main thread automatically.
  3. So you can use Handler to send messages to the Activity class to let it modify the UI component.
  4. Because the UI component is thread-unsafe, only the main thread can modify it.
  5. Please read Android Handler Example to learn more.

2. How To Create Child Thread’s Message Queue And Looper.

  1. However, the worker thread created by default has no message queue and message looper, If you want the worker thread to have a message queue and message looper, you can follow the below steps.
  2. Call Looper.prepare() to create the message queue in the thread.
  3. Create a thread-specified Handler that handles messages in the message queue.
  4. Call Looper.loop() to enter the message loop.
  5. If you want the worker thread to quit the message loop, please call Handler.getLooper().quit().

3. Android Child Thread Message Queue And Looper Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/4hxQ5f0NoR8

  1. From the above video, you can see the below steps.
  2. When the first two buttons are clicked, the main thread will send a message object to the worker thread message queue.
  3. The worker thread read the message object out from the queue and sends a message to the main thread also.
  4. The main thread will display different text according to the worker thread sent messages.
  5. After the “quit child thread looper” button is clicked, the worker thread message looper stopped. And worker thread can not handle any messages. So the text view content will not change also.
  6. activity_child_thread_handler_looper.xml
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">
    
        <Button
            android:id="@+id/runTaskOneButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Run Task One" />
    
        <Button
            android:id="@+id/runTaskTwoButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Run Task Two" />
    
        <Button
            android:id="@+id/quitChildThreaLooperButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Quit Child Thread Looper" />
    
        <TextView
            android:id="@+id/taskStatusTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="" />
    </LinearLayout>
  7. ChildThreadHandlerLooperActivity.java
    package com.dev2qa.example;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class ChildThreadHandlerLooperActivity extends AppCompatActivity {
    
        private int MAIN_THREAD_TASK_1 = 1;
        private int MAIN_THREAD_TASK_2 = 2;
        private int CHILD_THREAD_QUIT_LOOPER = 3;
    
        private Handler mainThreadHandler;
    
        private MyWorkerThread workerThread = null;
    
        private Button runTaskOneButton;
    
        private Button runTaskTwoButton;
    
        private TextView taskStatusTextView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_child_thread_handler_looper);
    
            setTitle("dev2qa.com - Child Thread Looper Handler Example");
    
            // Create and start the worker thread.
            workerThread = new MyWorkerThread();
            workerThread.start();
    
            // Handle message from main thread message queue.
            mainThreadHandler = new Handler(Looper.getMainLooper()){
                @Override
                public void handleMessage(Message msg) {
                    Log.i("MAIN_THREAD", "Receive message from child thread.");
                    if(msg.what == MAIN_THREAD_TASK_1)
                    {
                        // If task one button is clicked.
                        taskStatusTextView.setText("Task one execute.");
                    }else if(msg.what == MAIN_THREAD_TASK_2)
                    {
                        // If task two button is clicked.
                        taskStatusTextView.setText("Task two execute.");
                    }else if(msg.what == CHILD_THREAD_QUIT_LOOPER)
                    {
                        // If quit child thread looper button is clicked.
                        taskStatusTextView.setText("Quit child thread looper.");
                    }
                }
            };
    
            // Get run task buttons.
            runTaskOneButton = (Button)findViewById(R.id.runTaskOneButton);
            runTaskTwoButton = (Button)findViewById(R.id.runTaskTwoButton);
    
            // Set on click listener to each button.
            runTaskOneButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // When click this button, create a message object.
                    Message msg = new Message();
                    msg.what = MAIN_THREAD_TASK_1;
                    // Use worker thread message Handler to put message into worker thread message queue.
                    workerThread.workerThreadHandler.sendMessage(msg);
                }
            });
    
            // Please see comments for runTaskOneButton.
            runTaskTwoButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Message msg = new Message();
                    msg.what = MAIN_THREAD_TASK_2;
                    workerThread.workerThreadHandler.sendMessage(msg);
                }
            });
    
            // Get status info TextView object.
            taskStatusTextView = (TextView)findViewById(R.id.taskStatusTextView);
    
            // Get the quit child thread looper button.
            Button quitChildThreadLooperButton = (Button)findViewById(R.id.quitChildThreaLooperButton);
            quitChildThreadLooperButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Click this button will quit child thread looper.
                    workerThread.workerThreadHandler.getLooper().quit();
                }
            });
        }
    
        // This child thread class has it's own Looper and Handler object.
        private class MyWorkerThread extends Thread{
            // This is worker thread handler.
            public Handler workerThreadHandler;
    
            @Override
            public void run() {
                // Prepare child thread Lopper object.
                Looper.prepare();
    
                // Create child thread Handler.
                workerThreadHandler = new Handler(Looper.myLooper()){
                    @Override
                    public void handleMessage(Message msg) {
                        // When child thread handler get message from child thread message queue.
                        Log.i("CHILD_THREAD", "Receive message from main thread.");
                        Message message = new Message();
                        message.what = msg.what;
                        // Send the message back to main thread message queue use main thread message Handler.
                        mainThreadHandler.sendMessage(message);
                    }
                };
                // Loop the child thread message queue.
                Looper.loop();
    
                // The code after Looper.loop() will not be executed until you call workerThreadHandler.getLooper().quit()
                Log.i("CHILD_THREAD", "This log is printed after Looper.loop() method. Only when this thread loop quit can this log be printed.");
                // Send a message to main thread.
                Message msg = new Message();
                msg.what = CHILD_THREAD_QUIT_LOOPER;
                mainThreadHandler.sendMessage(msg);
            }
        }
    }
  8. From the android studio Logcat console output, we can see that before worker thread looper’s quit() method is called, the code after Looper.loop() in child thread will not be executed.
  9. The main thread Logcat console data output.
    com.dev2qa.example I/MAIN_THREAD: Receive message from child thread.
    com.dev2qa.example I/MAIN_THREAD: Receive message from child thread.
    com.dev2qa.example I/MAIN_THREAD: Receive message from child thread.
    com.dev2qa.example I/MAIN_THREAD: Receive message from child thread.
    com.dev2qa.example I/MAIN_THREAD: Receive message from child thread.
    com.dev2qa.example I/MAIN_THREAD: Receive message from child thread.
  10. The child thread Logcat console data output.
    com.dev2qa.example I/CHILD_THREAD: Receive message from main thread.
    com.dev2qa.example I/CHILD_THREAD: Receive message from main thread.
    com.dev2qa.example I/CHILD_THREAD: Receive message from main thread.
    com.dev2qa.example I/CHILD_THREAD: Receive message from main thread.
    com.dev2qa.example I/CHILD_THREAD: Receive message from main thread.
    com.dev2qa.example I/CHILD_THREAD: Receive message from main thread.
    com.dev2qa.example I/CHILD_THREAD: This log is printed after Looper.loop() method. Only when this thread loop quit can this log be printed.
    

3 thoughts on “Android Thread Message Looper Handler Example”

  1. Thanks you so much for this example.
    It’s too helpful and simple to understand the working of loopers and handlers.

  2. I have one doubt in the code. In your code, after clicking button it is sending message object to the message queue of child thread and then from the child thread using UIthread handler object it is sending message object to UIthread message queue and by using main looper it is showing text in UI thread. Am I right?

  3. Thank your for your share,
    I’m confused about looper, handler, message issue and search for some example to learn about it.

    Your example is simple but very useful to me.
    Thank you very much!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.