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

# TokenWatcher

*** ** * ** ***

` public abstract class TokenWatcher `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

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

<br />

*** ** * ** ***

A TokenWatcher watches a collection of `https://developer.android.com/reference/android/os/IBinder`s. IBinders are added to the collection by calling `https://developer.android.com/reference/android/os/TokenWatcher#acquire(android.os.IBinder,%20java.lang.String)`, and removed by calling `https://developer.android.com/reference/android/os/TokenWatcher#release(android.os.IBinder)`. IBinders are also implicitly removed when they become weakly reachable. Each IBinder may be added at most once. The `https://developer.android.com/reference/android/os/TokenWatcher#acquired()` method is invoked by posting to the specified handler whenever the size of the watched collection becomes nonzero. The `https://developer.android.com/reference/android/os/TokenWatcher#released()` method is invoked on the specified handler whenever the size of the watched collection becomes zero.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/os/TokenWatcher#TokenWatcher(android.os.Handler,%20java.lang.String)(https://developer.android.com/reference/android/os/Handler h, https://developer.android.com/reference/java/lang/String tag) ` Construct the TokenWatcher |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/os/TokenWatcher#acquire(android.os.IBinder,%20java.lang.String)(https://developer.android.com/reference/android/os/IBinder token, https://developer.android.com/reference/java/lang/String tag) ` Record that this token has been acquired. |
| ` abstract void` | ` https://developer.android.com/reference/android/os/TokenWatcher#acquired()() ` Called when the number of active tokens goes from 0 to 1. |
| ` void` | ` https://developer.android.com/reference/android/os/TokenWatcher#cleanup(android.os.IBinder,%20boolean)(https://developer.android.com/reference/android/os/IBinder token, boolean unlink) ` |
| ` void` | ` https://developer.android.com/reference/android/os/TokenWatcher#dump(java.io.PrintWriter)(https://developer.android.com/reference/java/io/PrintWriter pw) ` |
| ` void` | ` https://developer.android.com/reference/android/os/TokenWatcher#dump()() ` |
| ` boolean` | ` https://developer.android.com/reference/android/os/TokenWatcher#isAcquired()() ` |
| ` void` | ` https://developer.android.com/reference/android/os/TokenWatcher#release(android.os.IBinder)(https://developer.android.com/reference/android/os/IBinder token) ` |
| ` abstract void` | ` https://developer.android.com/reference/android/os/TokenWatcher#released()() ` Called when the number of active tokens goes from 1 to 0. |

### 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 constructors

### TokenWatcher

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

```
public TokenWatcher (Handler h, 
                String tag)
```

Construct the TokenWatcher

<br />

| Parameters ||
|---|---|
| `h` | `Handler`: A handler to call `https://developer.android.com/reference/android/os/TokenWatcher#acquired()` and `https://developer.android.com/reference/android/os/TokenWatcher#released()` on. If you don't care, just call it like this, although your thread will have to be a Looper thread. `new TokenWatcher(new Handler())` <br /> |
| `tag` | `String`: A debugging tag for this TokenWatcher <br /> |

## Public methods

### acquire

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

```
public void acquire (IBinder token, 
                String tag)
```

Record that this token has been acquired. When acquire is called, and the current count is 0, the acquired method is called on the given handler. Note that the same `token` can only be acquired once. If this `token` has already been acquired, no action is taken. The first subsequent call to `https://developer.android.com/reference/android/os/TokenWatcher#release(android.os.IBinder)` will release this `token` immediately.

<br />

| Parameters ||
|---|---|
| `token` | `IBinder`: An IBinder object. <br /> |
| `tag` | `String`: A string used by the `https://developer.android.com/reference/android/os/TokenWatcher#dump()` method for debugging, to see who has references. <br /> |

### acquired

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

```
public abstract void acquired ()
```

Called when the number of active tokens goes from 0 to 1.

<br />

### cleanup

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

```
public void cleanup (IBinder token, 
                boolean unlink)
```

<br />

<br />

| Parameters ||
|---|---|
| `token` | `IBinder` <br /> |
| `unlink` | `boolean` <br /> |

### dump

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

```
public void dump (PrintWriter pw)
```

<br />

<br />

| Parameters ||
|---|---|
| `pw` | `PrintWriter` <br /> |

### dump

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

```
public void dump ()
```

<br />

<br />

### isAcquired

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

```
public boolean isAcquired ()
```

<br />

<br />

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

### release

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

```
public void release (IBinder token)
```

<br />

<br />

| Parameters ||
|---|---|
| `token` | `IBinder` <br /> |

### released

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

```
public abstract void released ()
```

Called when the number of active tokens goes from 1 to 0.

<br />