android4.4 系统时间同步,添加NTP备份地址功能
2016-12-07 10:44
836 查看
添加NTP备份地址功能,同步cn.pool.ntp.org服务器时间失败的话,可同步129.6.15.28服务器的时间,双保险。
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 81b388d..ae3e286 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -2867,6 +2867,7 @@ public final class Settings { MOVED_TO_GLOBAL.add(Settings.Global.NITZ_UPDATE_SPACING); MOVED_TO_GLOBAL.add(Settings.Global.NTP_SERVER); MOVED_TO_GLOBAL.add(Settings.Global.NTP_TIMEOUT); + MOVED_TO_GLOBAL.add(Settings.Global.NTP_BACKUP_SERVER); MOVED_TO_GLOBAL.add(Settings.Global.PDP_WATCHDOG_ERROR_POLL_COUNT); MOVED_TO_GLOBAL.add(Settings.Global.PDP_WATCHDOG_LONG_POLL_INTERVAL_MS); MOVED_TO_GLOBAL.add(Settings.Global.PDP_WATCHDOG_MAX_PDP_RESET_FAIL_COUNT); @@ -5074,6 +5075,8 @@ public final class Settings { /** Preferred NTP server. {@hide} */ public static final String NTP_SERVER = "ntp_server"; + /** Preferred NTP backup server. {@hide} */ + public static final String NTP_BACKUP_SERVER = "ntp_server2"; /** Timeout in milliseconds to wait for NTP server. {@hide} */ public static final String NTP_TIMEOUT = "ntp_timeout"; diff --git a/core/java/android/util/NtpTrustedTime.java b/core/java/android/util/NtpTrustedTime.java index 602a68c..d1bd18c 100644 --- a/core/java/android/util/NtpTrustedTime.java +++ b/core/java/android/util/NtpTrustedTime.java @@ -38,6 +38,9 @@ public class NtpTrustedTime implements TrustedTime { private final String mServer; private final long mTimeout; + private String mBackupServer = null; + private boolean mHasCache; private long mCachedNtpTime; private long mCachedNtpElapsedRealtime; @@ -49,6 +52,11 @@ public class NtpTrustedTime implements TrustedTime { mTimeout = timeout; } + private void setBackupServer(String server) + { + mBackupServer = server; + } + public static synchronized NtpTrustedTime getInstance(Context context) { if (sSingleton == null) { final Resources res = context.getResources(); @@ -56,6 +64,8 @@ public class NtpTrustedTime implements TrustedTime { final String defaultServer = res.getString( com.android.internal.R.string.config_ntpServer); + final String defaultServer2 = res.getString( + com.android.internal.R.string.config_ntpServer2); final long defaultTimeout = res.getInteger( com.android.internal.R.integer.config_ntpTimeout); @@ -64,8 +74,16 @@ public class NtpTrustedTime implements TrustedTime { final long timeout = Settings.Global.getLong( resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout); + final String secureServer2 = Settings.Global.getString( + resolver, Settings.Global.NTP_BACKUP_SERVER); + final String server = secureServer != null ? secureServer : defaultServer; + final String backupServer = secureServer2 != null ? secureServer2 : defaultServer2; sSingleton = new NtpTrustedTime(server, timeout); + if(sSingleton != null) + { + sSingleton.setBackupServer(backupServer); + } } return sSingleton; @@ -80,7 +98,8 @@ public class NtpTrustedTime implements TrustedTime { if (LOGD) Log.d(TAG, "forceRefresh() from cache miss"); final SntpClient client = new SntpClient(); - if (client.requestTime(mServer, (int) mTimeout)) { + if (client.requestTime(mServer, (int) mTimeout) || + (mBackupServer != null && client.requestTime(mBackupServer, (int) mTimeout))) { mHasCache = true; mCachedNtpTime = client.getNtpTime(); mCachedNtpElapsedRealtime = client.getNtpTimeReference(); diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index b7dd977..88550e1 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -1060,7 +1060,9 @@ <bool name="config_allowActionMenuItemTextWithIcon">false</bool> <!-- 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">cn.pool.ntp.org</string> + <!-- Remote server that can provide NTP backup responses. --> + <string translatable="false" name="config_ntpServer2">129.6.15.28</string> <!-- Normal polling frequency in milliseconds --> <integer name="config_ntpPollingInterval">864000000</integer> <!-- Try-again polling interval in milliseconds, in case the network request failed --> diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 84e6cce..957a821 100755 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -477,6 +477,7 @@ <java-symbol type="string" name="config_mms_user_agent" /> <java-symbol type="string" name="config_mms_user_agent_profile_url" /> <java-symbol type="string" name="config_ntpServer" /> + <java-symbol type="string" name="config_ntpServer2" /> <java-symbol type="string" name="config_tether_apndata" /> <java-symbol type="string" name="config_useragentprofile_url" /> <java-symbol type="string" name="config_wifi_p2p_device_type" /> diff --git a/core/java/android/net/SntpClient.java b/core/java/android/net/SntpClient.java index 316440f..b46b632 100644 --- a/core/java/android/net/SntpClient.java +++ b/core/java/android/net/SntpClient.java @@ -120,7 +120,7 @@ public class SntpClient mNtpTimeReference = responseTicks; mRoundTripTime = roundTripTime; } catch (Exception e) { - if (false) Log.d(TAG, "request time failed: " + e); + if (/*false*/true) Log.d(TAG, "request time failed: " + e); return false; } finally { if (socket != null) {
相关文章推荐
- linux系统时间同步关闭防火墙及阿里NTP地址
- 用ntp 网络同步 CentOS 系统时间
- C# NTP 时钟同步,获取指定IP地址的系统时间及错误问题解决。
- 系统时间同步及ntp服务的提供
- 系统时间同步服务器地址收集
- 运维之红帽管理员篇-----4. NTP时间同步 、 tar备份与恢复 、 管理用户和组 、 cron计划任务
- ADMIN05 - 管理用户和组、tar备份和恢复、NTP时间同步
- [RTC]系统时间NTP和RTC同步,Debian的时区配置
- CentOS系统时间同步(NTP)
- ntp 同步系统时间
- 系统时间同步服务器地址收集
- 系统时间同步服务器地址收集
- arm移植NTP(同步嵌入式linux系统时间)
- 系统时间同步服务器地址收集及简单配置
- 网络时间同步系统(NTP授时服务器)在电信网络的技术性应用研究
- NTP时间同步 、 tar备份与恢复 、 管理用户和组
- 系统时间同步及ntp服务的提供
- NTP系统时间同步-操作记录
- Linux运维之道之admin1.3(NTP时间同步,tar备份与恢复,管理用户和组)
- 系统时间同步服务器地址收集