Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/android/os/Looper#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/os/Looper#inhmethods)

# Looper

*** ** * ** ***

` public final class Looper `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.os.Looper |

<br />

*** ** * ** ***

Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call `https://developer.android.com/reference/android/os/Looper#prepare()` in the thread that is to run the loop, and then `https://developer.android.com/reference/android/os/Looper#loop()` to have it process messages until the loop is stopped.

Most interaction with a message loop is through the `https://developer.android.com/reference/android/os/Handler` class.

This is a typical example of the implementation of a Looper thread, using the separation of `https://developer.android.com/reference/android/os/Looper#prepare()` and `https://developer.android.com/reference/android/os/Looper#loop()` to create an initial Handler to communicate with the Looper.

```
 class LooperThread extends Thread {
     public Handler mHandler;

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

         mHandler = new Handler(Looper.myLooper()) {
             public void handleMessage(Message msg) {
                 // process incoming messages here
             }
         };

         Looper.loop();
     }
 }
```

<br />

## Summary

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/os/Looper#dump(android.util.Printer,%20java.lang.String)(https://developer.android.com/reference/android/util/Printer pw, https://developer.android.com/reference/java/lang/String prefix) ` Dumps the state of the looper for debugging purposes. |
| ` static https://developer.android.com/reference/android/os/Looper` | ` https://developer.android.com/reference/android/os/Looper#getMainLooper()() ` Returns the application's main looper, which lives in the main thread of the application. |
| ` https://developer.android.com/reference/android/os/MessageQueue` | ` https://developer.android.com/reference/android/os/Looper#getQueue()() ` Gets this looper's message queue. |
| ` https://developer.android.com/reference/java/lang/Thread` | ` https://developer.android.com/reference/android/os/Looper#getThread()() ` Gets the Thread associated with this Looper. |
| ` boolean` | ` https://developer.android.com/reference/android/os/Looper#isCurrentThread()() ` Returns true if the current thread is this looper's thread. |
| ` static void` | ` https://developer.android.com/reference/android/os/Looper#loop()() ` Run the message queue in this thread. |
| ` static https://developer.android.com/reference/android/os/Looper` | ` https://developer.android.com/reference/android/os/Looper#myLooper()() ` Return the Looper object associated with the current thread. |
| ` static https://developer.android.com/reference/android/os/MessageQueue` | ` https://developer.android.com/reference/android/os/Looper#myQueue()() ` Return the `https://developer.android.com/reference/android/os/MessageQueue` object associated with the current thread. |
| ` static void` | ` https://developer.android.com/reference/android/os/Looper#prepare()() ` Initialize the current thread as a looper. |
| ` static void` | ` https://developer.android.com/reference/android/os/Looper#prepareMainLooper()() ` *This method was deprecated in API level 30. The main looper for your application is created by the Android environment, so you should never need to call this function yourself.* |
| ` void` | ` https://developer.android.com/reference/android/os/Looper#quit()() ` Quits the looper. |
| ` void` | ` https://developer.android.com/reference/android/os/Looper#quitSafely()() ` Quits the looper safely. |
| ` void` | ` https://developer.android.com/reference/android/os/Looper#setMessageLogging(android.util.Printer)(https://developer.android.com/reference/android/util/Printer printer) ` Control logging of messages as they are processed by this Looper. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/os/Looper#toString()() ` Returns a string representation of the object. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public methods

### dump

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void dump (Printer pw, 
                String prefix)
```

Dumps the state of the looper for debugging purposes.

<br />

| Parameters ||
|---|---|
| `pw` | `Printer`: A printer to receive the contents of the dump. This value cannot be `null`. <br /> |
| `prefix` | `String`: A prefix to prepend to each line which is printed. This value cannot be `null`. <br /> |

### getMainLooper

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static Looper getMainLooper ()
```

Returns the application's main looper, which lives in the main thread of the application.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/Looper` | <br /> |

### getQueue

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MessageQueue getQueue ()
```

Gets this looper's message queue.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/MessageQueue` | The looper's message queue. This value cannot be `null`. <br /> |

### getThread

Added in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Thread getThread ()
```

Gets the Thread associated with this Looper.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/Thread` | The looper's thread. This value cannot be `null`. <br /> |

### isCurrentThread

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isCurrentThread ()
```

Returns true if the current thread is this looper's thread.

<br />

| Returns ||
|---|---|
| `boolean` | <br /> |

### loop

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static void loop ()
```

Run the message queue in this thread. Be sure to call `https://developer.android.com/reference/android/os/Looper#quit()` to end the loop.

<br />

### myLooper

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static Looper myLooper ()
```

Return the Looper object associated with the current thread. Returns null if the calling thread is not associated with a Looper.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/Looper` | <br /> |

### myQueue

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static MessageQueue myQueue ()
```

Return the `https://developer.android.com/reference/android/os/MessageQueue` object associated with the current thread. This must be called from a thread running a Looper, or a NullPointerException will be thrown.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/MessageQueue` | This value cannot be `null`. <br /> |

### prepare

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static void prepare ()
```

Initialize the current thread as a looper. This gives you a chance to create handlers that then reference this looper, before actually starting the loop. Be sure to call `https://developer.android.com/reference/android/os/Looper#loop()` after calling this method, and end it by calling `https://developer.android.com/reference/android/os/Looper#quit()`.

<br />

### prepareMainLooper

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static void prepareMainLooper ()
```

<br />

**This method was deprecated in API level 30.**   

The main looper for your application is created by the Android environment, so you should never need to call this function yourself.

Initialize the current thread as a looper, marking it as an application's main looper. See also: `https://developer.android.com/reference/android/os/Looper#prepare()`

<br />

### quit

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void quit ()
```

Quits the looper.

Causes the `https://developer.android.com/reference/android/os/Looper#loop()` method to terminate without processing any more messages in the message queue.

Any attempt to post messages to the queue after the looper is asked to quit will fail. For example, the `https://developer.android.com/reference/android/os/Handler#sendMessage(android.os.Message)` method will return false.

If `https://developer.android.com/reference/android/os/Looper#quit()` or `https://developer.android.com/reference/android/os/Looper#quitSafely()` is called multiple times, the first call will have an effect and the subsequent calls will be no-ops.

Using this method may be unsafe because some messages may not be delivered before the looper terminates. Consider using `https://developer.android.com/reference/android/os/Looper#quitSafely()` instead to ensure that all pending work is completed in an orderly manner.

<br />

<br />

**See also:**

- `https://developer.android.com/reference/android/os/Looper#quitSafely()`

### quitSafely

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void quitSafely ()
```

Quits the looper safely.

Causes the `https://developer.android.com/reference/android/os/Looper#loop()` method to terminate as soon as all remaining messages in the message queue that are already due to be delivered have been handled. However pending delayed messages with due times in the future will not be delivered before the loop terminates.

Any attempt to post messages to the queue after the looper is asked to quit will fail. For example, the `https://developer.android.com/reference/android/os/Handler#sendMessage(android.os.Message)` method will return false.

If `https://developer.android.com/reference/android/os/Looper#quit()` or `https://developer.android.com/reference/android/os/Looper#quitSafely()` is called multiple times, the first call will have an effect and the subsequent calls will be no-ops.

<br />

<br />

### setMessageLogging

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setMessageLogging (Printer printer)
```

Control logging of messages as they are processed by this Looper. If enabled, a log message will be written to <var translate="no">printer</var> at the beginning and ending of each message dispatch, identifying the target Handler and message contents. Message logging introduces a performance penalty, and is disabled by default.

<br />

| Parameters ||
|---|---|
| `printer` | `Printer`: A Printer object that will receive log messages, or null to disable message logging. <br /> |

### toString

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a string representation of the object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |