SntpClient
@UnstableApi
public final class SntpClient
Static utility to retrieve the device time offset using SNTP.
Based on the Android framework SntpClient.
Summary
Nested types |
|---|
public interface SntpClient.InitializationCallbackCallback for calls to |
Constants |
|
|---|---|
static final String |
DEFAULT_NTP_HOST = "time.android.com"The default NTP host address used to retrieve |
static final int |
DEFAULT_TIMEOUT_MS = 1000The default maximum time, in milliseconds, to wait for the SNTP request to complete. |
Public fields |
|
|---|---|
static long |
@GuardedBy(value = "valueLock") |
static boolean |
@GuardedBy(value = "valueLock") |
static long |
@GuardedBy(value = "valueLock") |
static String |
|
static int |
Public methods |
|
|---|---|
static long |
Returns the offset between |
static long |
Returns the maximum time to elapse until the client is re-initialized, in milliseconds. |
static String |
Returns the NTP host address used to retrieve |
static int |
Returns the maximum time to wait for the SNTP request to complete, in milliseconds. |
static void |
initialize(Starts loading the device time offset. |
static boolean |
Returns whether the device time offset has already been loaded. |
static void |
setMaxElapsedTimeUntilUpdateMs(long maxElapsedTimeUntilUpdateMs)Sets the maximum time to elapse until the client is re-initialized, in milliseconds. |
static void |
setNtpHost(String ntpHost)Sets the NTP host address used to retrieve |
static void |
setTimeoutMs(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 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
maxElapsedTimeUntilUpdateMs
@GuardedBy(value = "valueLock")
public static long maxElapsedTimeUntilUpdateMs
Public methods
getElapsedRealtimeOffsetMs
public static long getElapsedRealtimeOffsetMs()
Returns the offset between elapsedRealtime and the NTP server time in milliseconds, or TIME_UNSET if 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 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 | |
|---|---|
@Nullable Loader loader |
A |
@Nullable SntpClient.InitializationCallback callback |
An optional |
isInitialized
public static boolean isInitialized()
Returns whether the device time offset has already been loaded.
If false, use initialize 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 TIME_UNSET to never re-initialize.
setNtpHost
public static void setNtpHost(String ntpHost)
Sets the NTP host address used to retrieve 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.
| Parameters | |
|---|---|
String 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 DEFAULT_TIMEOUT_MS.
If the new timeout is different from the previous one, the NTP client will be isInitialized uninitialized} again.