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

相关推荐
jzlhll12313 小时前
android kotlin Flow:distinctUntilChangedBy + stateIn 的坑
android·开发语言·kotlin
美狐美颜sdk13 小时前
直播APP开发如何实现美颜功能?低成本美颜SDK方案推荐
android·人工智能·ios·第三方美颜sdk·视频美颜sdk
程序员JerrySUN14 小时前
Jetson边缘嵌入式实战课程第五讲:Jetson Secure Boot - 安全启动
android·linux·服务器·人工智能·安全·unity·游戏引擎
zh_xuan14 小时前
Android JNI 动态注册:获取系统内存页大小
android·cmake·jni·ndk·动态注册·内存页大小
CocoaKier15 小时前
X未提前通知,突然停用twitter授权登录域名,大量X三方登录异常!
android·ios
0pen115 小时前
android-sqlite3:从官方 SQLite 源码自动构建 Android 可用的 sqlite3
android·数据库·sqlite
测试开发-学习笔记16 小时前
adb命令
android·adb
plainGeekDev16 小时前
Android四大组件面试题,看完这篇就够了
android·面试·kotlin
私人珍藏库16 小时前
【Android】Todesk手机远控手机、电脑,无会员无广告!!
android·学习·智能手机·app·工具·软件·多功能