Understanding Android's Network Time Detection: Ensuring Accurate Device Time
Android devices rely on network time detection to automatically set the correct time. This article explores how this system works, its configuration, and how you can ensure optimal accuracy.
How Android's Network Time Detection Works
Android uses the Simple Network Time Protocol (SNTP) over UDP to get time information from network time servers. This system, known as the network time origin, updates the device's system clock automatically.
The network_time_update_service
service, running within the Android system server, is responsible for this process. It periodically uses SNTP to obtain time and monitors network connectivity to trigger refreshes when needed.
By default, network time is the primary source for automatic time detection in Android. Let's examine the key components and configurations.
Configuring the Network Time Detection System
The network_time_update_service
sends time suggestions to the time_detector
service using internal APIs. The time_detector
then decides whether to update the system clock based on configured priority rules.
To prioritize network time, ensure that network
is included in config_autoTimeSourcesPriority
within the core/res/res/values/config.xml
file. See Time Source Priority for detailed information.
Key Device Configuration Options
Device manufacturers can fine-tune network time detection behavior using specific configuration keys found in frameworks/base/core/res/res/values/config.xml
:
Configuration Key | AOSP Value | Description |
---|---|---|
config_ntpRetry |
3 |
Number of retries with shorter NTP polling interval (config_ntpPollingIntervalShorter ) before reverting to the normal interval (config_ntpPollingInterval ). |
config_ntpPollingInterval |
64800000 (18 hours) |
Normal network time polling interval in milliseconds. |
config_ntpPollingIntervalShorter |
60000 (1 minute) |
Retry network time polling interval in milliseconds, used after a failed refresh. |
config_ntpServers |
ntp://time.android.com |
NTP servers to use. Format: ntp://<server>[:port] . |
config_ntpTimeout |
5000 |
Time to wait for an NTP server response in milliseconds before timeout. |
Leveraging Multiple NTP Servers
Starting with Android 14, the framework supports multiple NTP servers. This allows devices distributed globally to maintain accurate time even when access to time.android.com
is restricted. The system tries each server in the config_ntpServers
list until it finds one that responds.
If a responding server is found, the system will stick with that server until a refresh fails or the device restarts.
Understanding Time Accuracy
Android uses SNTP, performing a single daily time query to maintain recent time signals. Network latency significantly impacts time accuracy. SNTP assumes symmetric network delays, but asymmetric delays in mobile networks can increase inaccuracy. With the default config_ntpTimeout
of 5000 milliseconds, the maximum theoretical error is about 2.5 seconds if all latency is concentrated on one leg.
System clock accuracy also depends on the device's ability to track elapsed time after a time signal is received. The network_time_update_service
refreshes regularly to keep the time_detector
service supplied with fresh time suggestions.
Other Uses of Network Time
Even when automatic time detection is disabled, network time is still used by:
- The
SystemClock.currentNetworkTimeClock()
method. - Internal platform functions like A-GPS for faster location fixes.
Debugging and Testing Network Time Detection
Use the following shell commands for debugging and testing:
-
To dump the current state of
network_time_update_service
:adbshellcmdnetwork_time_update_servicedump
-
To see command-line options for testing:
adbshellcmdnetwork_time_update_servicehelp