# ListenerSet

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/util/ListenerSet.java+class:androidx.media3.common.util.ListenerSet)

*** ** * ** ***


```
@UnstableApi
public final class ListenerSet<T extends Object>
```

<br />

*** ** * ** ***

A set of listeners.

Events are guaranteed to arrive in the order in which they happened even if a new event is triggered recursively from another listener.

Events are also guaranteed to be only sent to the listeners registered at the time the event was enqueued and haven't been removed since.

All methods must be called on the thread passed to the constructor unless indicated otherwise.

| Parameters |
|---|---|
| `<T extends https://developer.android.com/reference/java/lang/Object.html>` | The listener type. |

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T>` An event sent to a listener. |
| `public interface https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent<T>` An event sent to a listener when all other events sent during one `https://developer.android.com/reference/android/os/Looper.html` message queue iteration were handled by the listener. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#ListenerSet(android.os.Looper)(https://developer.android.com/reference/android/os/Looper.html looper)` Creates a new listener set. |
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#ListenerSet(java.lang.Thread)(https://developer.android.com/reference/java/lang/Thread.html thread)` Creates a new listener set. |
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#ListenerSet(android.os.Looper,androidx.media3.common.util.Clock,androidx.media3.common.util.ListenerSet.IterationFinishedEvent<T>)( https://developer.android.com/reference/android/os/Looper.html looper, https://developer.android.com/reference/androidx/media3/common/util/Clock clock, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent )` Creates a new listener set. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#add(T)(T listener)` Adds a listener to the set. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#clear()()` Removes all listeners from the set. |
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet<T>` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#copy(androidx.media3.common.util.Clock)(https://developer.android.com/reference/androidx/media3/common/util/Clock clock)` Copies the listener set. |
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet<T>` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#copy(android.os.Looper)(https://developer.android.com/reference/android/os/Looper.html looper)` Copies the listener set. |
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet<T>` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#copy(android.os.Looper,androidx.media3.common.util.ListenerSet.IterationFinishedEvent<T>)( https://developer.android.com/reference/android/os/Looper.html looper, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent )` Copies the listener set. |
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet<T>` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#copy(android.os.Looper,androidx.media3.common.util.Clock,androidx.media3.common.util.ListenerSet.IterationFinishedEvent<T>)( https://developer.android.com/reference/android/os/Looper.html looper, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/util/Clock clock, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent )` Copies the listener set. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()()` Notifies listeners of events previously enqueued with `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(int,androidx.media3.common.util.ListenerSet.Event<T>)`. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#isRunningOnCorrectThread()()` Returns whether the current thread is the one expected by most methods of this class (unless documented otherwise). |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(androidx.media3.common.util.ListenerSet.Event<T>)(https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T> event)` Adds an event that is sent to the listeners when `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()` is called. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(int,androidx.media3.common.util.ListenerSet.Event<T>)(int eventFlag, https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T> event)` Adds an event that is sent to the listeners when `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()` is called. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#release()()` Releases the set of listeners immediately. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#remove(T)(T listener)` Removes a listener from the set. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#sendEvent(androidx.media3.common.util.ListenerSet.Event<T>)(https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T> event)` `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(androidx.media3.common.util.ListenerSet.Event<T>)` a single event and immediately `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()` the event queue to notify all listeners. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#sendEvent(int,androidx.media3.common.util.ListenerSet.Event<T>)(int eventFlag, https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T> event)` `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(int,androidx.media3.common.util.ListenerSet.Event<T>)` a single event and immediately `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()` the event queue to notify all listeners. |
| `void` | `[setThrowsWhenUsingWrongThread](https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#setThrowsWhenUsingWrongThread(boolean))(boolean throwsWhenUsingWrongThread)` **This method is deprecated.** Do not use this method and ensure all calls are made from the correct thread. <br /> |
| `int` | `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#size()()` Returns the number of added listeners. |

## Public constructors

### ListenerSet

```
public ListenerSet(Looper looper)
```

Creates a new listener set.

This listener set will not send an `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent` when all other events sent during one `https://developer.android.com/reference/android/os/Looper.html` message queue iteration were handled by the listeners.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | A `https://developer.android.com/reference/android/os/Looper.html` used to call listeners on. The same `https://developer.android.com/reference/android/os/Looper.html` must be used to call all other methods of this class unless indicated otherwise. |

### ListenerSet

```
public ListenerSet(Thread thread)
```

Creates a new listener set.

This listener set will not send an `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/Thread.html thread` | A `https://developer.android.com/reference/java/lang/Thread.html` used to call listeners on. The same `https://developer.android.com/reference/java/lang/Thread.html` must be used to call all other methods of this class unless indicated otherwise. |

### ListenerSet

```
public ListenerSet(
    Looper looper,
    Clock clock,
    @Nullable ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent
)
```

Creates a new listener set.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | A `https://developer.android.com/reference/android/os/Looper.html` used to call listeners on. The same `https://developer.android.com/reference/android/os/Looper.html` must be used to call all other methods of this class unless indicated otherwise. |
| `https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | A `https://developer.android.com/reference/androidx/media3/common/util/Clock`. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent` | An `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent` sent when all other events sent during one `https://developer.android.com/reference/android/os/Looper.html` message queue iteration were handled by the listeners, or null if no such event is needed. |

## Public methods

### add

```
public void add(T listener)
```

Adds a listener to the set.

If a listener is already present, it will not be added again.

This method can be called from any thread.

| Parameters |
|---|---|
| `T listener` | The listener to be added. |

### clear

```
public void clear()
```

Removes all listeners from the set.

### copy

```
public ListenerSet<T> copy(Clock clock)
```

Copies the listener set.

This method can be called from any thread.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | The new `https://developer.android.com/reference/androidx/media3/common/util/Clock` for the copied listener set. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet<T>` | The copied listener set. |

### copy

```
public ListenerSet<T> copy(Looper looper)
```

Copies the listener set.

This method can be called from any thread.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | The new `https://developer.android.com/reference/android/os/Looper.html` for the copied listener set. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet<T>` | The copied listener set. |

### copy

```
public ListenerSet<T> copy(
    Looper looper,
    @Nullable ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent
)
```

Copies the listener set.

This method can be called from any thread.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | The new `https://developer.android.com/reference/android/os/Looper.html` for the copied listener set. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent` | The new `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent` sent when all other events sent during one `https://developer.android.com/reference/android/os/Looper.html` message queue iteration were handled by the listeners, or null if no such event is needed. Can only be non-null if the listener set was created with a `https://developer.android.com/reference/androidx/media3/common/util/Clock`. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet<T>` | The copied listener set. |

### copy

```
public ListenerSet<T> copy(
    Looper looper,
    @Nullable Clock clock,
    @Nullable ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent
)
```

Copies the listener set.

This method can be called from any thread.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | The new `https://developer.android.com/reference/android/os/Looper.html` for the copied listener set. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | The new `https://developer.android.com/reference/androidx/media3/common/util/Clock` for the copied listener set. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent` | The new `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent` sent when all other events sent during one `https://developer.android.com/reference/android/os/Looper.html` message queue iteration were handled by the listeners, or null if no such event is needed. Can only be non-null if the `clock` is non-null. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet<T>` | The copied listener set. |

### flushEvents

```
public void flushEvents()
```

Notifies listeners of events previously enqueued with `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(int,androidx.media3.common.util.ListenerSet.Event<T>)`.

### isRunningOnCorrectThread

```
public boolean isRunningOnCorrectThread()
```

Returns whether the current thread is the one expected by most methods of this class (unless documented otherwise).

### queueEvent

```
public void queueEvent(ListenerSet.Event<T> event)
```

Adds an event that is sent to the listeners when `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()` is called.

This call does not set an integer flag for this event to be reported in the `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent`. Use `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(int,androidx.media3.common.util.ListenerSet.Event<T>)` instead if this is required.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T> event` | The event. |

### queueEvent

```
public void queueEvent(int eventFlag, ListenerSet.Event<T> event)
```

Adds an event that is sent to the listeners when `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()` is called.

| Parameters |
|---|---|
| `int eventFlag` | An integer indicating the type of the event to be reported in the `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent`, or `https://developer.android.com/reference/androidx/media3/common/C#INDEX_UNSET()` to report this event without flag. |
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T> event` | The event. |

### release

```
public void release()
```

Releases the set of listeners immediately.

This will ensure no events are sent to any listener after this method has been called.

### remove

```
public void remove(T listener)
```

Removes a listener from the set.

If the listener is not present, nothing happens.

| Parameters |
|---|---|
| `T listener` | The listener to be removed. |

### sendEvent

```
public void sendEvent(ListenerSet.Event<T> event)
```

`https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(androidx.media3.common.util.ListenerSet.Event<T>)` a single event and immediately `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()` the event queue to notify all listeners.

This call does not set an integer flag for this event to be reported in the `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent`. Use `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#sendEvent(int,androidx.media3.common.util.ListenerSet.Event<T>)` instead if this is required.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T> event` | The event. |

### sendEvent

```
public void sendEvent(int eventFlag, ListenerSet.Event<T> event)
```

`https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#queueEvent(int,androidx.media3.common.util.ListenerSet.Event<T>)` a single event and immediately `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#flushEvents()` the event queue to notify all listeners.

| Parameters |
|---|---|
| `int eventFlag` | An integer flag indicating the type of the event to be reported in the `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.IterationFinishedEvent`, or `https://developer.android.com/reference/androidx/media3/common/C#INDEX_UNSET()` to report this event without flag. |
| `https://developer.android.com/reference/androidx/media3/common/util/ListenerSet.Event<T> event` | The event. |

### setThrowsWhenUsingWrongThread

```
public void [setThrowsWhenUsingWrongThread](https://developer.android.com/reference/androidx/media3/common/util/ListenerSet#setThrowsWhenUsingWrongThread(boolean))(boolean throwsWhenUsingWrongThread)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Do not use this method and ensure all calls are made from the correct thread.

Sets whether methods throw when using the wrong thread.

Do not use this method unless to support legacy use cases.

| Parameters |
|---|---|
| `boolean throwsWhenUsingWrongThread` | Whether to throw when using the wrong thread. |

### size

```
public int size()
```

Returns the number of added listeners.