Automatic update time mechanism in android4.4
Glossary:
NITZ: NITZ (Network Identity and Time Zone) or Network Identity and Time Zone, is a mechanism used to automatically configure local Time and date, in addition, carrier information is also provided to mobile devices through wireless networks. NITZ is an optional function in GSM of PHASE 2 + RELEASE 96 and is often used to automatically update the system clock of mobile phones.
NTP: NTP (Network Time Protocol) provides accurate Time. First, it must have accurate Time sources, which should be international standard Time UTC. NTP obtains UTC time from atomic clocks, astronomical observatory, and satellites, or from the Internet. In this way, an accurate and reliable time source is available. The time is distributed according to the NTP server level.
1. Select "automatic time and date" in setting, and assign values to the Preference with the key value AUTO_TIME and AUTO_TIME_ZONE.
Source code path: packages/apps/Settings/src/com/android/settings/DateTimeSettings. java
2. the preceding key value is monitored in/frameworks/base/services/java/com/android/server/NetworkTimeUpdateService. When the key value is detected to change, the message mHandler will be sent. obtainMessage (mMsg ). sendToTarget ();
void observe(Context context) { ContentResolver resolver = context.getContentResolver(); resolver.registerContentObserver(Settings.Global.getUriFor(Settings.Global.AUTO_TIME), false, this); } @Override public void onChange(boolean selfChange) { mHandler.obtainMessage(mMsg).sendToTarget(); }
After receiving the message, handler calls onPollNetworkTime (msg. what) and finds that both data and wifi call this method for update.
public void handleMessage(Message msg) { switch (msg.what) { case EVENT_AUTO_TIME_CHANGED: case EVENT_POLL_NETWORK_TIME: case EVENT_NETWORK_CONNECTED: onPollNetworkTime(msg.what); break; } }
3. in the onPollNetworkTime method, first check whether the "Automatic update time" option is selected. If it is not selected, exit. If it is selected again, if the updated NITZ time is not NOT_SET (-1 ), if the update interval is less than mPollingIntervalMs and mPollingIntervalMs is 24 hours, the NITZ time is directly updated. Otherwise, the NTP synchronization time is used.
// If NITZ time was received less than mPollingIntervalMs time ago,// no need to sync to NTP.if (mNitzTimeSetTime != NOT_SET && refTime - mNitzTimeSetTime < mPollingIntervalMs) { resetAlarm(mPollingIntervalMs); return; }
final long ntp = mTime.currentTimeMillis(); mTryAgainCounter = 0; // If the clock is more than N seconds off or this is the first time it's been // fetched since boot, set the current time. if (Math.abs(ntp - currentTime) > mTimeErrorThresholdMs || mLastNtpFetchTime == NOT_SET) { // Set the system time ...... if (ntp / 1000 < Integer.MAX_VALUE) { SystemClock.setCurrentTimeMillis(ntp); }
When the absolute value of the difference between the NTP time obtained from the time server and the current time is greater than a threshold value, the system determines that the current time is incorrect and the Update time is required.
Summary:
If the automatic time synchronization option is not checked, return directly;
If NITZ has been synchronized and the last NITZ synchronization does not exceed 24 hours, the timer is set to 24 hours before the synchronization is triggered, that is, NetworkTimeUpdateService. ACTION_POLL is broadcast;
If the last time NTP was successfully synchronized for more than 24 hours or the user selected the automatic synchronization option, the following NTP synchronization will be performed. Otherwise, the synchronization will be triggered after the timer is set for 24 hours;
If the last NTP synchronization has been successful for more than 24 hours, mTime. forceRefresh () will be initiated ();
If the synchronization succeeds, obtain the NTP time ntp = mTime. currentTimeMillis ();
If the synchronization Time deviation from the current local time exceeds the specified threshold value, set ntp to the local time SystemClock. setCurrentTimeMillis (ntp)