What is the purpose of Looper and how to use it?

Khawar Raza

I am new to Android. I want to know what the Looper class does and also how to use it. I have read the Android Looper class documentation but I am unable to completely understand it. I have seen it in a lot of places but unable to understand its purpose. Can anyone help me by defining the purpose of Looper and also by giving a simple example if possible?

Dharmendra

What is Looper?

Looper is a class which is used to execute the Messages(Runnables) in a queue. Normal threads have no such queue, e.g. simple thread does not have any queue. It executes once and after method execution finishes, the thread will not run another Message(Runnable).

Where we can use Looper class?

If someone wants to execute multiple messages(Runnables) then he should use the Looper class which is responsible for creating a queue in the thread. For example, while writing an application that downloads files from the internet, we can use Looper class to put files to be downloaded in the queue.

How it works?

There is prepare() method to prepare the Looper. Then you can use loop() method to create a message loop in the current thread and now your Looper is ready to execute the requests in the queue until you quit the loop.

Here is the code by which you can prepare the Looper.

class LooperThread extends Thread {
      public Handler mHandler;

      @Override
      public void run() {
          Looper.prepare();

          mHandler = new Handler() {
              @Override
              public void handleMessage(Message msg) {
                  // process incoming messages here
              }
          };

          Looper.loop();
      }
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the purpose and use of **kwargs?

From Dev

What is the purpose of ServletContext setInitParameter? How can one use it?

From Dev

what is the purpose of use abstract state?

From Dev

What is the use/purpose of MQTT QoS?

From Dev

What purpose to use union in struct?

From Dev

purpose of PPAPI toolchain and how to use it?

From Dev

What does purpose use #ifdef and #if in C++

From Dev

What is the purpose, and proper use of wantTo() in Codeception?

From Dev

What is the use/purpose of the ca-certificates package?

From Dev

What is the use/purpose of the ca-certificates package?

From Dev

Why use looper in thread

From Java

What is the purpose of Node.js module.exports and how do you use it?

From Dev

What is the purpose of configuring a software bridge in RHEL? How do we use it in virtualization?

From Dev

What is the purpose of Yield and how does it work?

From Dev

What is the purpose of .bashrc and how does it work?

From Dev

how the Main method is used and what it's purpose is?

From Dev

What is the purpose of .*\\?

From Dev

What is the purpose of "?"

From Dev

What is the best practice to get Looper?

From Dev

How to clarify the purpose of its use in the location modal?

From Dev

How to clarify the purpose of its use in the location modal?

From Dev

Since LocalDB is for development purpose, then use what DB for production?

From Java

What is the purpose of the var keyword and when should I use it (or omit it)?

From Dev

What is the purpose of "Add CREATE DATABASE / USE statement" option in PHPMyAdmin Export

From Dev

What is the purpose of "use curl --cookie with a file that doesn't exist"?

From Dev

What's the purpose of having express use webpack dev server?

From Dev

What is the purpose of emplace() if we can use vector[1] = someInt?

From Java

Docker, what is it and what is the purpose

From Dev

How exactly using the Looper() and Handler()

Related Related

  1. 1

    What is the purpose and use of **kwargs?

  2. 2

    What is the purpose of ServletContext setInitParameter? How can one use it?

  3. 3

    what is the purpose of use abstract state?

  4. 4

    What is the use/purpose of MQTT QoS?

  5. 5

    What purpose to use union in struct?

  6. 6

    purpose of PPAPI toolchain and how to use it?

  7. 7

    What does purpose use #ifdef and #if in C++

  8. 8

    What is the purpose, and proper use of wantTo() in Codeception?

  9. 9

    What is the use/purpose of the ca-certificates package?

  10. 10

    What is the use/purpose of the ca-certificates package?

  11. 11

    Why use looper in thread

  12. 12

    What is the purpose of Node.js module.exports and how do you use it?

  13. 13

    What is the purpose of configuring a software bridge in RHEL? How do we use it in virtualization?

  14. 14

    What is the purpose of Yield and how does it work?

  15. 15

    What is the purpose of .bashrc and how does it work?

  16. 16

    how the Main method is used and what it's purpose is?

  17. 17

    What is the purpose of .*\\?

  18. 18

    What is the purpose of "?"

  19. 19

    What is the best practice to get Looper?

  20. 20

    How to clarify the purpose of its use in the location modal?

  21. 21

    How to clarify the purpose of its use in the location modal?

  22. 22

    Since LocalDB is for development purpose, then use what DB for production?

  23. 23

    What is the purpose of the var keyword and when should I use it (or omit it)?

  24. 24

    What is the purpose of "Add CREATE DATABASE / USE statement" option in PHPMyAdmin Export

  25. 25

    What is the purpose of "use curl --cookie with a file that doesn't exist"?

  26. 26

    What's the purpose of having express use webpack dev server?

  27. 27

    What is the purpose of emplace() if we can use vector[1] = someInt?

  28. 28

    Docker, what is it and what is the purpose

  29. 29

    How exactly using the Looper() and Handler()

HotTag

Archive