android 网络自动同步时间慢问题

本文讨论了网络时间同步的问题,提出了将默认的ntp服务器从time.android.com更改为cn.pool.ntp.org以提高在中国大陆的访问效率,并调整config_ntpPollingIntervalShorter值来改变同步频率的方法。

问题描述:

今天测试提了一个网络同步时间慢的bug。

网络同步时间原理参考: https://blog.youkuaiyun.com/yin1031468524/article/details/65447849

核心代码在NetworkTimeUpdateService.java , NtpTrustedTime,  SntpClient中

frameworks\base\core\res\res\values\config.xml中有核心配置相关项。其中

config_ntpServer 是默认的ntp服务器,mtk源代码中是time.android.com, 但是大陆访问不了,把地址改为 cn.pool.ntp.org 即可。

另外修改config_ntpPollingIntervalShorter 值去修改同步更新时间的频率。

常用NTP服务器

类型NTP 地址说明
公共 NTP 池pool.ntp.org全球智能分发,推荐使用
区域 NTP 池0.pool.ntp.org / 1.pool.ntp.org / 2.pool.ntp.org / 3.pool.ntp.org提供负载均衡和冗余
Google NTPtime.google.com稳定可靠,支持 IPv6
Cloudflare NTPtime.cloudflare.com延迟低、安全性高(支持 NTS)
NIST(美国)time.nist.gov官方时间源,但部分地区访问较慢
中国国家授时中心(建议中国用户使用)ntp.ntsc.ac.cn中国科学院提供,国内访问快
阿里云 NTP(中国用户)ntp.aliyun.com快速、稳定,阿里 CDN 网络
腾讯云 NTP(中国用户)ntp.tencent.com类似阿里云,适合国内设备使用

本地测试情况看:

time.cloudflare.com 

2.android.pool.ntp.org

较为稳定。

可先用adb 命令测试下不同服务器同步网络时间

adb shell settings put global ntp_server ntp.ntsc.ac.cn  //更改服务器地址
adb shell setprop persist.sys.timezone Asia/Shangha  //更改时区
adb shell settings get global ntp_server  //查看当前使用地址
adb shell settings put global auto_time 1  //打开网络同步时间开关
adb shell settings put global auto_time 0  //关闭网络同步时间开关

adb logcat -s NtpTrustedTime SntpClient NetworkTimeUpdateService

配置多个服务器参考:

https://blog.youkuaiyun.com/weixin_45639314/article/details/136700746

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值