赞
踩
基于高通平台、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>
二、在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();
系统编译烧录后,时间可以更新成功。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。