Skip to content

Instantly share code, notes, and snippets.

Last active April 17, 2024 03:40
Show Gist options
  • Save xujiaao/63cb3bbea9fe22e79206e5eb7ba82d0e to your computer and use it in GitHub Desktop.
Save xujiaao/63cb3bbea9fe22e79206e5eb7ba82d0e to your computer and use it in GitHub Desktop.
Set the NTP server of your android device
Android Things

Set the NTP server of your android device

Just use this command to set a server address to automatically sync your device time. ( ͡° ͜ʖ ͡°)✧

$ adb shell settings put global ntp_server <new-ntp-server>


I have a Raspberry Pi 3 with Android Things installed on it. But the time is never synchronized...

After googling it, I found this piece of code in

public class NtpTrustedTime implements TrustedTime {
    public static synchronized NtpTrustedTime getInstance(Context context) {
        if (sSingleton == null) {
            final Resources res = context.getResources();
            final ContentResolver resolver = context.getContentResolver();
            final String defaultServer = res.getString(
            final long defaultTimeout = res.getInteger(
            final String secureServer = Settings.Global.getString(
                    resolver, Settings.Global.NTP_SERVER);
            final long timeout = Settings.Global.getLong(
                    resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout);
            final String server = secureServer != null ? secureServer : defaultServer;
            sSingleton = new NtpTrustedTime(server, timeout);
            sContext = context;
        return sSingleton;

It shows that android tring to get NTP server from:

  1. Settings.Global.NTP_SERVER first

  2. Then

And the NTP server of my Android Things is, that's why it dose not work (I'm in China...).

Fortunately, it is easy to set settings via adb:

$ adb shell settings put global ntp_server

# test
$ adb shell settings get global ntp_server 


Copy link

You sir, are a gentleman and a scholar.

Copy link

ipcjs commented Dec 1, 2022 is a good choice in China.

Copy link

don-dolarson commented May 26, 2023

Thank you for this thread.

There is no existing ntp_server setting in LineageOS 20 but can easily be created just by pushing the "put global ntp_server domain" command through adb.

Setting is preserved between reboots and my router logs can confirm that NTP address changed from to the chosen NTP server. GPS automatically connects to (Cloudflare) in this ROM.

Copy link

wodeguaiguai commented Jul 21, 2023

Failed to execute this command, my devices is android 12 colorOS, it seems no write permisson

➜ /Users/guaio >adb shell settings put global ntp_server

Exception occurred while executing 'put':
java.lang.SecurityException: Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
	at android.content.ContentProvider$
	at android.os.ShellCommand.exec(
	at android.os.Binder.shellCommand(
	at android.os.Binder.onTransact(
	at android.os.Binder.execTransactInternal(
	at android.os.Binder.execTransact(

Copy link

Thanks for the information. I've tried several servers, is the fastest one in China.

Copy link

In my device if I first of all do adb shell settings get global ntp_server, it returns null.

Copy link

costaht commented Jan 10, 2024

For those trying to connect to your TV, I used adb connect to connect to my tv via LAN. Once you run that command you're gonna have to click Accept on the pop-up that is shown on your tv screen.
adb devices should show your TV as active, then you can run the commands above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment