问题描述:
今天测试提了一个网络同步时间慢的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 NTP | time.google.com | 稳定可靠,支持 IPv6 |
Cloudflare NTP | time.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