Android 10 4G网络不能校时问题分析

本文探讨了如何检查设备到NTP服务器的网络连接,通过ping测试确认网络可达性,并重点解析config.xml和NtpTrustedTime.java中的配置参数。解决网络时间同步问题,包括可能的运营商限制及动态添加备用NTP服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、设备到ntp服务器的网络是否正常

130|test:/ # ping 2.android.pool.ntp.org
PING 2.android.pool.ntp.org (202.112.29.82) 56(84) bytes of data.
64 bytes from dns1.synet.edu.cn (202.112.29.82): icmp_seq=1 ttl=47 time=94.5 ms

出现如上日志说明是可以ping通的,可以正常访问网络
二、域名和IP查询网址

https://site.ip138.com/2.android.pool.ntp.org/

域名查询
在这里插入图片描述IP查询
在这里插入图片描述总结:
1、如果该卡不能ping通域名,换一张卡可以ping通,则说明是该卡运营商那边有问题,运营商那边是否有做限制
2、从上面可以ping通域名的设备看,每次ping域名的时候,IP是随机分配的
解决方法:
代码路径:
frameworks/base/core/res/res/values/config.xml
frameworks/base/core/java/android/util/NtpTrustedTime.java
frameworks/base/services/core/java/com/android/server/NetworkTimeUpdateService.java
NetworkTimeUpdateService.java文件是网络更新时间处理,在config.xml资源文件中可以对其它参数可以配置,比如重连次数,超时时间的、域名

<!-- Remote server that can provide NTP responses. -->
    <string translatable="false" name="config_ntpServer">2.android.pool.ntp.org</string>
    <!-- Normal polling frequency in milliseconds -->
    <integer name="config_ntpPollingInterval">86400000</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>
    <!-- If the time difference is greater than this threshold in milliseconds,
         then update the time. -->
    <integer name="config_ntpThreshold">5000</integer>
    <!-- Timeout to wait for NTP server response in milliseconds. -->
    <integer name="config_ntpTimeout">5000</integer>

    <!-- Default network policy warning threshold, in megabytes. -->
    <integer name="config_networkPolicyDefaultWarning">2048</integer>

在NtpTrustedTime.java文件中forceRefresh()方法中可以动态添加NTP服务器域名

 @Override
    public boolean forceRefresh() {
        if (TextUtils.isEmpty(mServer)) {
            // missing server, so no trusted time available
            return false;
        }

        // We can't do this at initialization time: ConnectivityService might not be running yet.
        synchronized (this) {
            if (mCM == null) {
                mCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            }
        }

        final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo();
        if (ni == null || !ni.isConnected()) {
            if (LOGD) Log.d(TAG, "forceRefresh: no connectivity");
            return false;
        }


        if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
        final SntpClient client = new SntpClient();
        if (client.requestTime(mServer, (int) mTimeout)) {
            mHasCache = true;
            mCachedNtpTime = client.getNtpTime();
            mCachedNtpElapsedRealtime = client.getNtpTimeReference();
            mCachedNtpCertainty = client.getRoundTripTime() / 2;
            return true;
        } else {
            return false;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值