赞
踩
最近在项目中遇到了设备开机联网后不会主动更新时间的情况。由于上层app需要登录服务器,登录过程会对时间进行校验,时间不对服务器登录不成功。所以好好的研究了一下android的时间更新原理。具体原理不细说,网络上讲的很详细。针对项目来做一个简单的说明。
主要代码有:
/frameworks/base/services/core/java/com/android/server/NetworkTimeUpdateService.java
/frameworks/base/core/java/android/util/NtpTrustedTime.java
/frameworks/base/core/java/android/net/SntpClient.java
/frameworks/base/core/res/res/values/config.xml
针对项目中出现的问题,主要修改config.xml
<!-- Remote server that can provide NTP responses. -->
- <string translatable="false" name="config_ntpServer">2.android.pool.ntp.org</string>
+ <string translatable="false" name="config_ntpServer">ntp1.aliyun.com</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>
+ <integer name="config_ntpPollingIntervalShorter">30000</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>
+ <integer name="config_ntpRetry">5</integer>
<!-- Timeout to wait for NTP server response in milliseconds. -->
<integer name="config_ntpTimeout">5000</integer>
测试了一下,原生的ntp服务器2.android.pool.ntp.org访问成功率是相对低一些,所以将config_ntpServer 改为国内ali的ntp1.aliyun.com(还有其他可选),请求时间间隔改短(value < 0 代表一直请求),retry次数增加。
还可以考虑在frameworks/base/core/java/android/util/NtpTrustedTime.java 中增加备用ntp服务器。
参考网上代码:
--- a/frameworks/base/core/java/android/util/NtpTrustedTime.java +++ b/frameworks/base/core/java/android/util/NtpTrustedTime.java @@ -98,6 +98,16 @@ public class NtpTrustedTime implements TrustedTime { private static NtpTrustedTime sSingleton; + String[] backupNtpServers = new String[]{ + "ntp2.aliyun.com", + "ntp3.aliyun.com", + "ntp4.aliyun.com", + "ntp5.aliyun.com", + "ntp6.aliyun.com", + }; + int index = -1; + @NonNull private final Context mContext; @@ -162,12 +172,21 @@ public class NtpTrustedTime implements TrustedTime { if (LOGD) Log.d(TAG, "forceRefresh() from cache miss"); final SntpClient client = new SntpClient(); - final String serverName = connectionInfo.getServer(); + // final String serverName = connectionInfo.getServer(); + boolean result = false; + String serverName = connectionInfo.getServer(); final int timeoutMillis = connectionInfo.getTimeoutMillis(); - if (client.requestTime(serverName, timeoutMillis, network)) { + // if (client.requestTime(serverName, timeoutMillis, network)) { + while (!(result = client.requestTime(serverName, timeoutMillis, network)) && index < (backupNtpServers.length-1) ) { + index++; + serverName = backupNtpServers[index]; + if (LOGD) Log.d(TAG, "-----------add--serverName--------"+serverName); + } + if(result){ long ntpCertainty = client.getRoundTripTime() / 2; - mTimeResult = new TimeResult( - client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty); + // mTimeResult = new TimeResult( + // client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty); + mTimeResult = new TimeResult(client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty); return true; } else { return false;
重新烧录后,时间更新成功率变高了。但是仍不能保证100%更新成功。这也是网络时间同步的缺陷。如果想要保证一定更新可以考虑增加rtc。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。