# SntpClient

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

*** ** * ** ***


```
@UnstableApi
public final class SntpClient
```

<br />

*** ** * ** ***

Static utility to retrieve the device time offset using SNTP.

Based on the [Android framework SntpClient](https://cs.android.com/android/_/android/platform/frameworks/base/+/ea1b85a5eb6e9dcddf7abc6c74479bfadf9017c7:core/java/android/net/SntpClient.java).

## Summary

| ### Nested types |
|---|
| `public interface https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient.InitializationCallback` Callback for calls to `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#initialize(androidx.media3.exoplayer.upstream.Loader,androidx.media3.exoplayer.util.SntpClient.InitializationCallback)`. |

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#DEFAULT_NTP_HOST() = "time.android.com"` The default NTP host address used to retrieve `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getElapsedRealtimeOffsetMs()`. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#DEFAULT_TIMEOUT_MS() = 1000` The default maximum time, in milliseconds, to wait for the SNTP request to complete. |

| ### Public fields |
|---|---|
| `static long` | `@https://developer.android.com/reference/androidx/annotation/GuardedBy(value = "valueLock") https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#elapsedRealtimeOffsetMs()` |
| `static boolean` | `@https://developer.android.com/reference/androidx/annotation/GuardedBy(value = "valueLock") https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#isInitialized()` |
| `static long` | `@https://developer.android.com/reference/androidx/annotation/GuardedBy(value = "valueLock") https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#maxElapsedTimeUntilUpdateMs()` |
| `static https://developer.android.com/reference/java/lang/String.html` | `@https://developer.android.com/reference/androidx/annotation/GuardedBy(value = "valueLock") https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#ntpHost()` |
| `static int` | `@https://developer.android.com/reference/androidx/annotation/GuardedBy(value = "valueLock") https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#timeoutMs()` |

| ### Public methods |
|---|---|
| `static long` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getElapsedRealtimeOffsetMs()()` Returns the offset between `https://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime()` and the NTP server time in milliseconds, or `https://developer.android.com/reference/androidx/media3/common/C#TIME_UNSET()` if `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#isInitialized()` returns false. |
| `static long` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getMaxElapsedTimeUntilUpdateMs()()` Returns the maximum time to elapse until the client is re-initialized, in milliseconds. |
| `static https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getNtpHost()()` Returns the NTP host address used to retrieve `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getElapsedRealtimeOffsetMs()`. |
| `static int` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getTimeoutMs()()` Returns the maximum time to wait for the SNTP request to complete, in milliseconds. |
| `static void` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#initialize(androidx.media3.exoplayer.upstream.Loader,androidx.media3.exoplayer.util.SntpClient.InitializationCallback)( @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader loader, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient.InitializationCallback callback )` Starts loading the device time offset. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#isInitialized()()` Returns whether the device time offset has already been loaded. |
| `static void` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#setMaxElapsedTimeUntilUpdateMs(long)(long maxElapsedTimeUntilUpdateMs)` Sets the maximum time to elapse until the client is re-initialized, in milliseconds. |
| `static void` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#setNtpHost(java.lang.String)(https://developer.android.com/reference/java/lang/String.html ntpHost)` Sets the NTP host address used to retrieve `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getElapsedRealtimeOffsetMs()`. |
| `static void` | `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#setTimeoutMs(int)(int timeoutMs)` Sets the maximum time to wait for the SNTP request to complete, in milliseconds. |

## Constants

### DEFAULT_NTP_HOST

```
public static final String DEFAULT_NTP_HOST = "time.android.com"
```

The default NTP host address used to retrieve `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getElapsedRealtimeOffsetMs()`.

### DEFAULT_TIMEOUT_MS

```
public static final int DEFAULT_TIMEOUT_MS = 1000
```

The default maximum time, in milliseconds, to wait for the SNTP request to complete.

## Public fields

### elapsedRealtimeOffsetMs

```
@GuardedBy(value = "valueLock")
public static long elapsedRealtimeOffsetMs
```

### isInitialized

```
@GuardedBy(value = "valueLock")
public static boolean isInitialized
```

### maxElapsedTimeUntilUpdateMs

```
@GuardedBy(value = "valueLock")
public static long maxElapsedTimeUntilUpdateMs
```

### ntpHost

```
@GuardedBy(value = "valueLock")
public static String ntpHost
```

### timeoutMs

```
@GuardedBy(value = "valueLock")
public static int timeoutMs
```

## Public methods

### getElapsedRealtimeOffsetMs

```
public static long getElapsedRealtimeOffsetMs()
```

Returns the offset between `https://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime()` and the NTP server time in milliseconds, or `https://developer.android.com/reference/androidx/media3/common/C#TIME_UNSET()` if `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#isInitialized()` returns false.

The offset is calculated as `ntpServerTime - deviceElapsedRealTime`.

### getMaxElapsedTimeUntilUpdateMs

```
public static long getMaxElapsedTimeUntilUpdateMs()
```

Returns the maximum time to elapse until the client is re-initialized, in milliseconds.

### getNtpHost

```
public static String getNtpHost()
```

Returns the NTP host address used to retrieve `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getElapsedRealtimeOffsetMs()`.

### getTimeoutMs

```
public static int getTimeoutMs()
```

Returns the maximum time to wait for the SNTP request to complete, in milliseconds.

### initialize

```
public static void initialize(
    @Nullable Loader loader,
    @Nullable SntpClient.InitializationCallback callback
)
```

Starts loading the device time offset.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader loader` | A `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Loader` to use for loading the time offset, or null to create a new one. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient.InitializationCallback callback` | An optional `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient.InitializationCallback` to be notified when the time offset has been initialized or initialization failed. |

### isInitialized

```
public static boolean isInitialized()
```

Returns whether the device time offset has already been loaded.

If `false`, use `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#initialize(androidx.media3.exoplayer.upstream.Loader,androidx.media3.exoplayer.util.SntpClient.InitializationCallback)` to start the initialization.

### setMaxElapsedTimeUntilUpdateMs

```
public static void setMaxElapsedTimeUntilUpdateMs(long maxElapsedTimeUntilUpdateMs)
```

Sets the maximum time to elapse until the client is re-initialized, in milliseconds.

The default is `https://developer.android.com/reference/androidx/media3/common/C#TIME_UNSET()` to never re-initialize.

### setNtpHost

```
public static void setNtpHost(String ntpHost)
```

Sets the NTP host address used to retrieve `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#getElapsedRealtimeOffsetMs()`.

The default is `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#DEFAULT_NTP_HOST()`.

If the new host address is different from the previous one, the NTP client will be `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#isInitialized()` uninitialized} again.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html ntpHost` | The NTP host address. |

### setTimeoutMs

```
public static void setTimeoutMs(int timeoutMs)
```

Sets the maximum time to wait for the SNTP request to complete, in milliseconds.

The default is `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#DEFAULT_TIMEOUT_MS()`.

If the new timeout is different from the previous one, the NTP client will be `https://developer.android.com/reference/androidx/media3/exoplayer/util/SntpClient#isInitialized()` uninitialized} again.