由于Android系统默认的时间服务器为time.android.com,这个服务器可能在某些国家可能无法访问,这时候我们就需要重新指定新的时间服务器。
- 关于时间服务器
时间服务器可以在这里找到: https://www.ntppool.org
可以跟据自己所在的区域,选择就近的服务器或者是直接使用pool.ntp.org(https://www.ntppool.org/en/use.html):
driftfile /var/lib/ntp/ntp.drift server 0.pool.ntp.org server 1.pool.ntp.org server 2.pool.ntp.org server 3.pool.ntp.org
- 通过修改frameworks中的相关配置来指定
相关的代码在frameworks/base/core/java/android/util/NtpTrustedTime.java中:
public static synchronized NtpTrustedTime getInstance(Context context) { if (sSingleton == null) { final Resources res = context.getResources(); final ContentResolver resolver = context.getContentResolver(); final String defaultServer = res.getString( com.android.internal.R.string.config_ntpServer); final long defaultTimeout = res.getInteger( com.android.internal.R.integer.config_ntpTimeout); final String secureServer = Settings.Global.getString( resolver, Settings.Global.NTP_SERVER); final long timeout = Settings.Global.getLong( resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout); final String server = secureServer != null ? secureServer : defaultServer; sSingleton = new NtpTrustedTime(server, timeout); sContext = context; } return sSingleton; }
从这里可以看到,指定新的时间服务器,一个是可能通过修改frameworks/base/core/res/res/values/config.xml文件,修改config_ntpServer的值:
<!-- Remote server that can provide NTP responses. --> <string translatable="false" name="config_ntpServer">time.android.com</string>
如果有需要,我们还可以修改其他ntp相关的参数:
<!-- 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> <!-- 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> <!-- If the time difference is greater than this threshold in milliseconds, then update the time. --> <integer name="config_ntpThreshold">5000</integer> <!-- Timeout to wait for NTP server response in milliseconds. --> <integer name="config_ntpTimeout">5000</integer>
- 通过修改系统配置来指定
Android原生系统没有提供相应的设置界面来手动指定ntp服务器,需要通过执行命令来实现。
在开机之后,可能过如下命令更新ntp服务器:
$ adb shell settings put global ntp_server 0.pool.ntp.org
设置完成之后,需要重启一下Android framework, 可通过adb shell stop && adb shell start实现。
- 相关的参考文档:
- https://www.ntppool.org
- https://www.ntppool.org/en/use.html