Automatic update time mechanism in android4.4

Source: Internet
Author: User

Noun Explanation:

The Nitz:nitz (Network identity and time zone), or networking identity and time zone, is a mechanism for automatically configuring local times and dates, as well as providing carrier information to mobile devices via wireless networks. Nitz is an optional feature in GSM since phase 96, and is often used to automatically update the system clocks of mobile phones.

NTP:NTP (Network time Protocol) provides accurate time, first of all, to have an accurate time source, which should be the International standard UTC. The time source for NTP to get UTC can be atomic clocks, observatories, satellites, or it can be obtained from the Internet. This gives you an accurate and reliable time source. Time is propagated at the level of the NTP server.


1. Tick "Automatically determine time and date" in setting, and "Automatically determine time zone" only assigns a value of preference with key value of Auto_time and Auto_time_zone.

Source Path: Packages/apps/settings/src/com/android/settings/datetimesettings.java


2. The above key value is monitored in/frameworks/base/services/java/com/android/server/networktimeupdateservice, when the key value change is detected, The message mhandler.obtainmessage (MMSG) will be sent. 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 ();        

Handler received the message after the message processing call Onpollnetworktime (Msg.what), found whether the data or WiFi is called to update the method

       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 determine whether to check the "Automatic Update Time", if not checked directly exit, if checked and then look, if the updated Nitz time is not not_set (-1), and the update interval is less than Mpollingintervalms, Mpollingintervalms=24 hours, then directly update the Nitz time, otherwise synchronize time with NTP.

If NITZ time is received less than Mpollingintervalms time ago,//no need to sync to ntp.if (mnitztimesettime! = not_s ET && 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 Ti Me          ...           if (ntp/1000 < Integer.max_value) {                        systemclock.setcurrenttimemillis (NTP);         }

When the absolute value of the difference between the NTP time and the current time obtained from the time server is greater than a threshold, the system considers the current time error and needs to update the time.


Summarize:
If the time Auto sync option is unchecked, return directly;
If the Nitz is synchronized and the last Nitz synchronization does not exceed 24 hours, set the timer 24 hours before triggering the synchronization, that is, broadcast networktimeupdateservice.action_poll;
If NTP last successfully synchronized more than 24 hours or the user check the automatic synchronization option, then the following NTP synchronization, otherwise set the timer 24 hours before triggering synchronization;
If the last NTP was successfully synchronized for more than 24 hours, the synchronization Mtime.forcerefresh () is initiated;
If the synchronization succeeds, get the NTP Time Ntp=mtime.currenttimemillis () at this moment;
Set NTP to native time Systemclock.setcurrenttimemillis (NTP) if the synchronization time and the current local time error exceed the specified value threshold

Automatic update time mechanism in android4.4

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.