Network Time Synchronization in Android

Source: Internet
Author: User

In Android system settings, the option of automatic synchronization of network time is available. After broncho A1 is transplanted to Froyo, we find that the time synchronization option is invalid. So I spent a little time studying the Network Time Synchronization Process of Android. I was surprised by the research results. The Network Time Synchronization of Android has nothing to do with the SNTP protocol, and even has nothing to do with the TCP/IP protocol.

As you can see from the set application, the option of automatic synchronization of network time only modifies the settings. system. auto_time setting:

private void setAutoState(boolean isEnabled, boolean autotimeStatus) {       if (isEnabled == false) {           mAutoPref.setChecked(autotimeStatus);           mAutoPref.setEnabled(isEnabled);       }       else {           Settings.System.putInt(getContentResolver(),              Settings.System.AUTO_TIME, autotimeStatus ? 1 : 0);       }       mTimePref.setEnabled(!autotimeStatus);       mDatePref.setEnabled(!autotimeStatus);       mTimeZone.setEnabled(!autotimeStatus);    }

Who will use this setting? Then, find settings. system. auto_time in the code, which includes the following two parts:

telephony/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.javatelephony/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java

The implementation of GSM and CDMA should be similar. Here we just look at GSM:
1. Reference-RIL/reference-ril.c handles actively reported messages.

If (strstartswith (S, "% ctzv:") {/* ti specific -- nitz time */char * response; line = P = strdup (s ); at_tok_start (& P); err = at_tok_nextstr (& P, & response); free (line); If (Err! = 0) {LogE ("invalid nitz line % s/n", S);} else {ril_onunsolicitedresponse (ril_unsol_nitz_time_received, response, strlen (response ));}} here is the message actively reported by the processing module. If it is a time and time zone message, ril_onunsolicitedresponse is called. 2. ril_onunsolicitedresponse will send the message to the RIL client.
ret = sendResponse(p, client_id);

The format of time and time zone information is described in the definition of ril_unsol_nitz_time_received:

"Data" is const char * pointing to nitz time string in the form "YY/MM/DD, HH: mm: SS (+/-) tz, DT"
3. the RIL client processes the ril_unsol_nitz_time_received message (telephony/Java/COM/Android/Internal/telephony/RIL. Java: processunsolicited)

            case RIL_UNSOL_NITZ_TIME_RECEIVED:                if (RILJ_LOGD) unsljLogRet(response, ret);                // has bonus long containing milliseconds since boot that the NITZ                // time was received                long nitzReceiveTime = p.readLong();                Object[] result = new Object[2];                result[0] = ret;                result[1] = Long.valueOf(nitzReceiveTime);                if (mNITZTimeRegistrant != null) {                    mNITZTimeRegistrant                        .notifyRegistrant(new AsyncResult (null, result, null));                } else {                    // in case NITZ time registrant isnt registered yet                    mLastNITZTimeInfo = result;                }

Gsmservicestatetracker registers with RIL, so events are handled by gsmservicestatetracker.
4. gsmservicestatetracker handles event_nitz_time events:

            case EVENT_NITZ_TIME:                ar = (AsyncResult) msg.obj;                String nitzString = (String)((Object[])ar.result)[0];                long nitzReceiveTime = ((Long)((Object[])ar.result)[1]).longValue();                setTimeFromNITZString(nitzString, nitzReceiveTime);                break;

Here, nitzstring is a time string, which is parsed by settimefromnitzstring.

private void setTimeFromNITZString (String nitz, long nitzReceiveTime) {            String[] nitzSubs = nitz.split("[/:,+-]");            int year = 2000 + Integer.parseInt(nitzSubs[0]);            c.set(Calendar.YEAR, year);            // month is 0 based!            int month = Integer.parseInt(nitzSubs[1]) - 1;            c.set(Calendar.MONTH, month);            int date = Integer.parseInt(nitzSubs[2]);            c.set(Calendar.DATE, date);            int hour = Integer.parseInt(nitzSubs[3]);            c.set(Calendar.HOUR, hour);            int minute = Integer.parseInt(nitzSubs[4]);            c.set(Calendar.MINUTE, minute);

If you select to automatically synchronize the Network Time in the system settings, the system time will be set.

           if (getAutoTime()) {               setAndBroadcastNetworkSetTimeZone(zone.getID());           }           if (getAutoTime()) {setAndBroadcastNetworkSetTime(c.getTimeInMillis());           }

About nitz on Wiki:
Nitz, or network identity and time zone [1], is a mechanical for provisioning local time and date, as well as network provider identity information to mobile devices via a wireless network [2]. nitz has been part of the official GSM standard since phase 2 + release 96 [3]. nitz is often used to automatically update the system clock of mobile phones.
The nitz implementation is optional. If the operator does not support it, Android phones cannot use this function. In this case, it is best to use SNTP instead, otherwise the user will be confused. However, Android does not seem to do this currently. I only found two places to call sntpclient, but none of them set the system time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.