Android 修改ntp网络校时服务器

SystemService启动后创建NetworkTimeUpdateService网络校时服务,构造方法加载配置文件,默认配置文件位置在/frameworks/base/core/res/res/values/config.xml,定制部分放在/vendor方案商的定制目录。

XML 复制代码
    <!-- Remote server that can provide NTP responses. -->
    <string translatable="false" name="config_ntpServer">time.android.com</string>
    <!-- Normal polling frequency in milliseconds -->
    <integer name="config_ntpPollingInterval">64800000</integer>
    <!-- Try-again polling interval in milliseconds, in case the network request failed -->
    <integer name="config_ntpPollingIntervalShorter">60000</integer>
    <!-- Number of times to try again with the shorter interval, before backing
         off until the normal polling interval. A value < 0 indicates infinite. -->
    <integer name="config_ntpRetry">3</integer>
    <!-- Timeout to wait for NTP server response in milliseconds. -->
    <integer name="config_ntpTimeout">5000</integer>

config_ntpServer:默认NTP服务器,方案商定制可能换成2.android.pool.ntp.org

config_ntpPollingInterval:开机后的轮询间隔时间

NetworkTimeUpdateService在网络连接后,轮训校时。

打开frameworks/base/core/java/android/util/NtpTrustedTime.java,找到getNtpConnectionInfo方法:

java 复制代码
    private NtpConnectionInfo getNtpConnectionInfo() {
        .......................................
        final String hostname;
        if (mHostnameForTests != null) {
            hostname = mHostnameForTests;
        } else {
            String serverGlobalSetting =
                    Settings.Global.getString(resolver, Settings.Global.NTP_SERVER);
            if (serverGlobalSetting != null) {
                hostname = serverGlobalSetting;
            } else {
                hostname = res.getString(com.android.internal.R.string.config_ntpServer);
            }
        }
        ........................................
        return TextUtils.isEmpty(hostname) ? null :
            new NtpConnectionInfo(hostname, port, timeoutMillis);
    }

Settings.Global中NTP_SERVER的值:

public static final String NTP_SERVER = "ntp_server";

按照方法逻辑,当Settings.Global.NTP_SERVER的值为空时,系统取默认的ntp服务器。

修改ntp网络校时服务器就有两种选择:

1、不修改系统:

adb shell settings put global ntp_server xxx.xxx.xxx.xxx

xxx.xxx.xxx.xxx为服务器IP

adb shell settings get global ntp_server,查看修改后的值

2、修改系统

修改config_ntpServer的值

相关推荐
TechNomad8 小时前
Kotlin_Lambda编程详解
android·kotlin
熊猫_豆豆8 小时前
QT6 Android C++ 自制美观闹钟
android·c++·qt·闹钟
美狐美颜SDK开放平台8 小时前
Android/iOS直播APP平台开发中的视频美颜SDK优化技巧
android·ios·音视频·美颜sdk·第三方美颜sdk
聚美智数11 小时前
黄历查询-运势查询-国际法定节假日查询-API接口介绍
android·java·数据库
私人珍藏库13 小时前
[Android] 一木百宝箱-万能百宝箱+抖音去水印等几百种功能
android·人工智能·app·软件·多功能
wupa13 小时前
在 enableEdgeToEdge 模式下处理软键盘与自定义面板协同
android
plainGeekDev15 小时前
ProGuard → R8
android·java·kotlin
雨白16 小时前
C 语言字符串:从字符数组、指针到核心操作实战
android
Java小白笔记16 小时前
MySQL中存储过程大表分批删除历史数据
android·mysql·adb
aidou131417 小时前
Kotlin中沉浸式状态栏显示布局
android·kotlin·windowmanager·沉浸式状态栏·insetslistener·insetscompat·systembars