当前位置:   article > 正文

android11系统开发之ntp服务器_android 开发 设置ntp

android 开发 设置ntp

最近在项目中遇到了设备开机联网后不会主动更新时间的情况。由于上层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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

测试了一下,原生的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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

重新烧录后,时间更新成功率变高了。但是仍不能保证100%更新成功。这也是网络时间同步的缺陷。如果想要保证一定更新可以考虑增加rtc。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/808206
推荐阅读
相关标签
  

闽ICP备14008679号