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

相关推荐
艾莉丝努力练剑1 天前
【MYSQL】MYSQL学习的一大重点:基本查询(下)
android·数据库·学习·mysql·面试·八股文
三少爷的鞋1 天前
Android 中coroutineScope 和 supervisorScope 到底怎么选?先搞清楚 Exception 和 Result
android
其实防守也摸鱼1 天前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·运维·网络·人工智能·学习·安全·macos
YM52e1 天前
鸿蒙Flutter Padding内边距:EdgeInsets详解
android·学习·flutter·华为·harmonyos·鸿蒙
Rex叶然2 天前
ScreenMatch适配安卓屏幕脚本,用于老项目
android·screenmatch·安卓屏幕适配
阿pin2 天前
Android随笔-Activity启动流程深度分析
android·activity启动流程
zzq77972 天前
加固包闪退四象限定位:targetSdk 与保护策略实战解析
android·安全·安卓·安全架构
我命由我123452 天前
Android 开发问题:Cannot resolve method ‘repeat‘ in ‘String‘
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
光头闪亮亮2 天前
Fyne ( go跨平台GUI )项目实战-scanner摄像头扫码组件开发技术详解
android·go
Android打工仔2 天前
Continuation 到底是谁创建的?
android·kotlin