68

Do Android Devices use the network time protocol (NTP) to synchronize the time?

In my Device-Settings I see a checkbox with the following text "synchronize with network", but I don't know if they are using NTP.

I need this for my Bachelor Thesis for which I use GPS. To get an accurate GPS-signal the receiver should have an exact clock.

I have found this blog-entry on the web, but I'm not sure if he tells the truth: Speeding up NTP, GPS Lock in Android

Heba
  • 319
  • 1
  • 3
  • 11
iTamp
  • 793
  • 1
  • 8
  • 12

5 Answers5

57

I know about Android ICS that it uses a custom service called: NetworkTimeUpdateService. This service also implements a NTP time synchronization via the NtpTrustedTime singleton.

In NtpTrustedTime the default NTP server is requested from the Android system string source:

final Resources res = context.getResources();

final String defaultServer = res.getString(
                                com.android.internal.R.string.config_ntpServer);

If the automatic time sync option in the system settings is checked and no NITZ time service is available then the time will be synchronized with the NTP server from com.android.internal.R.string.config_ntpServer.

To get the value of com.android.internal.R.string.config_ntpServer you can use the following method:

    final Resources res = this.getResources();
    final int id = Resources.getSystem().getIdentifier(
                       "config_ntpServer", "string","android");
    final String defaultServer = res.getString(id);
Dyonisos
  • 3,541
  • 2
  • 22
  • 25
  • 16
    Interestingly, it looks like they've hard-coded it to **2.android.pool.ntp.org**. Line 820: https://android.googlesource.com/platform/frameworks/base/+/40caf8f4432acd2b9d9230b2b1371660521415c2/core/res/res/values/config.xml#802 – Jeremy Logan Jan 15 '14 at 17:17
  • 3
    Yes, but that is only for stock android. Some manufacturers change the server string. – Dyonisos Jan 15 '14 at 21:47
  • 10
    Notes: 1) If you want to change the ntp server, Android only looks at the config_ntpServer value on startup, so you'll need to reboot (or restart NetworkTimeUpdateService) to use the new value. 2) Since 2012, Android only checks the time every 10 days once booted. (There is a patch in gerrit to restore the original timeout of 1 day) 3) A trick to get Android to check the time is to toggle Settings.Global.AUTO_TIME off and then back on. This is equivalent to the check box in the Date & Time section of Settings.app. 4) Android doesn't use NTP, it polls very infrequently using SNTP. – Harvey Sep 30 '15 at 17:27
  • 3
    So when you say: "1) If you want to change the ntp server, Android only looks at the config_ntpServer value on startup, so you'll need to reboot (or restart NetworkTimeUpdateService) to use the new value", exactly HOW should I be able to change the value? – Tobi Apr 12 '16 at 14:06
12

i wanted to ask if Android Devices uses the network time protocol (ntp) to synchronize the time.

For general time synchronization, devices with telephony capability, where the wireless provider provides NITZ information, will use NITZ. My understanding is that NTP is used in other circumstances: NITZ-free wireless providers, WiFi-only, etc.

Your cited blog post suggests another circumstance: on-demand time synchronization in support of GPS. That is certainly conceivable, though I do not know whether it is used or not.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
10

I have a Samsung Galaxy Tab 2 7.0 with Android 4.1.1. Apparently it does NOT sync to ntp. I loaded an app that says my tablet is 20 seconds off of ntp, but it can't set it unless I root the device.

Mishka
  • 349
  • 1
  • 5
  • 10
  • 1
    Seems to be manual update only... Mine gains 1.5 minutes per 6 months. – Fiasco Labs Jul 09 '13 at 04:57
  • 1
    The Tab 2 has topped out with Android 4.2.2, and does not have the checkbox mentioned by @Harvey in his comment on the accepted answer. Mine drifts backward in time. I'll have to try GPS sync the next time I can't scrobble. – Mike C Oct 18 '15 at 21:01
7

Not an exact answer to your question, but a bit of information: if your device does use NTP for time (eg. if it is a tablet with no 3G or GPS capabilities), the server can be configured in /system/etc/gps.conf - obviously this file can only be edited with root access, but is viewable on non-rooted devices.

toryan
  • 435
  • 5
  • 15
  • Ok, the file is present on and complete on my Samsung Galaxy tab S. Do you know how I can enable the built-in NTP? – user2284570 Dec 29 '14 at 22:01
  • 1
    @user2284570 I think some of the answers above will give you more better information than I could at this point – toryan Jan 08 '15 at 20:06
  • There is no gps.conf file inside the / folder. What could be the reason? My device is rooted. – Hilal Jan 15 '20 at 17:25
  • @Hilal This question/answer is 6 years old, maybe Android has changed since then. Sorry but I'm not in a position to keep this answer up-to-date. – toryan Jan 17 '20 at 12:08
1

you can override ntp with a global variable NTP_SERVER, the property ro.NTP_SERVER, or by creating a file named ntp.conf under /etc as explained in:
https://developers.google.com/time/guides#linux_ntpd_or_chrony

the Linux instructions works well with Android.

you can always redirect the traffic from your device through proxy into a burp packet listener running on another computer on the same network to easily inspect all tratfic. SIM in your device will sync up regardless of NTP settings, and your router by default may also act as a NTP server for the network.