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的值

相关推荐
wawo0010 分钟前
Kuikly实践-从Android compose迁移到kuikly compose
android
Carson带你学Android2 小时前
Android 版 MCP 正式登场:AppFunctions
android·ai编程·jetbrains
古法安卓5 小时前
Android-Direct Boot 阶段与 getFilesDir() 路径变更问题深度分析
android·java·android studio
阿巴斯甜5 小时前
Android Studio Detekt 使用
android
杉氧5 小时前
Flutter 跨平台多端适配与 Android/iOS 一键自动化打包发布
android·前端·flutter
Super 含6 小时前
Android 包体积优化(一):APK 到底大在哪里?从 APK 结构到体积分析
android·ide·vscode
Super 含7 小时前
Android 包体积优化(四):深入 ReDex——StripDebugInfo、InterDex 与 DEX 重排
android
安卓与AI研习社7 小时前
主线程明明是空闲的,为什么仍然 ANR?3 个真实 Trace 的证据链复盘
android
恋猫de小郭8 小时前
Dart 3.13 大更新,感觉比 Flutter 更带劲
android·前端·flutter
大锅盖19 小时前
HarmonyOS 6.1.1 ArkWeb:交付门户下载资料前-为什么必须先登记下载代理与来源字段
android·华为·harmonyos