当前位置:   article > 正文

Android 11.0修改原生ntp服务器_修改android默认ntp服务器

修改android默认ntp服务器

基于高通平台、Android11.0源码,在项目开发中遇到开机启动后,联网无法自动同步时间问题,主要修改源码frameworks配置,代码修改位置主要有两处:

1、frameworks/base/core/java/android/util/NtpTrustedTime.java
2、frameworks/base/core/res/res/values/config.xm

一、目前Android 11源码中配的原生ntp服务器是time.android.com,访问成功率低或者直接请求失败,所以将config_ntpServer 改为国内ali的ntp1.aliyun.com(还有其他可选),请求时间间隔改短(value < 0 代表一直请求),retry次数增加。


--- a/frameworks/base/core/res/res/values/config.xml
+++ b/frameworks/base/core/res/res/values/config.xml
@@ -2040,14 +2040,14 @@
     <bool name="config_actionMenuItemAllCaps">true</bool>

     <!-- Remote server that can provide NTP responses. -->
-    <string translatable="false" name="config_ntpServer">time.android.com</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
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

二、在NtpTrustedTime.java中增加备用NTP服务器:

--- a/frameworks/base/core/java/android/util/NtpTrustedTime.java
+++ b/frameworks/base/core/java/android/util/NtpTrustedTime.java
@@ -99,6 +99,15 @@ 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;

@@ -194,18 +203,27 @@ public class NtpTrustedTime implements TrustedTime {

             if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
             final SntpClient client = new SntpClient();
+            //String serverName = connectionInfo.getServer();
+                       boolean result = false;
             String serverName = connectionInfo.getServer();
             final int timeoutMillis = connectionInfo.getTimeoutMillis();

-            if (getBackupmode()) {
-                setBackupmode(false);
-                serverName = mBackupServer;
-            }
+            //if (getBackupmode()) {
+            //    setBackupmode(false);
+            //    serverName = mBackupServer;
+            //}
             if (LOGD) Log.d(TAG, "Ntp Server to access at:" + serverName);
-            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 {
                 countInBackupmode();

  • 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
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

系统编译烧录后,时间可以更新成功。

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

闽ICP备14008679号