public final class SntpClient extends Object
Based on the Android framework SntpClient.
| Modifier and Type | Class | Description |
|---|---|---|
static interface |
SntpClient.InitializationCallback |
Callback for calls to
initialize(Loader, InitializationCallback). |
| Modifier and Type | Field | Description |
|---|---|---|
static String |
DEFAULT_NTP_HOST |
The default NTP host address used to retrieve
getElapsedRealtimeOffsetMs(). |
| Modifier and Type | Method | Description |
|---|---|---|
static long |
getElapsedRealtimeOffsetMs() |
Returns the offset between
SystemClock.elapsedRealtime() and the NTP server time in
milliseconds, or C.TIME_UNSET if isInitialized() returns false. |
static String |
getNtpHost() |
Returns the NTP host address used to retrieve
getElapsedRealtimeOffsetMs(). |
static void |
initialize(Loader loader,
SntpClient.InitializationCallback callback) |
Starts loading the device time offset.
|
static boolean |
isInitialized() |
Returns whether the device time offset has already been loaded.
|
static void |
setNtpHost(String ntpHost) |
Sets the NTP host address used to retrieve
getElapsedRealtimeOffsetMs(). |
public static final String DEFAULT_NTP_HOST
getElapsedRealtimeOffsetMs().public static String getNtpHost()
getElapsedRealtimeOffsetMs().public static void setNtpHost(String ntpHost)
getElapsedRealtimeOffsetMs().
The default is DEFAULT_NTP_HOST.
If the new host address is different from the previous one, the NTP client will be isInitialized() uninitialized} again.
ntpHost - The NTP host address.public static boolean isInitialized()
If false, use initialize(Loader, InitializationCallback) to start the
initialization.
public static long getElapsedRealtimeOffsetMs()
SystemClock.elapsedRealtime() and the NTP server time in
milliseconds, or C.TIME_UNSET if isInitialized() returns false.
The offset is calculated as ntpServerTime - deviceElapsedRealTime.
public static void initialize(@Nullable
Loader loader,
@Nullable
SntpClient.InitializationCallback callback)
loader - A Loader to use for loading the time offset, or null to create a new one.callback - An optional SntpClient.InitializationCallback to be notified when the time offset
has been initialized or initialization failed.