目录
一、时间同步模块启动
源码文件:frameworks/base/services/java/com/android/server/SystemServer.java
//这个是使能时间同步模块的配置,正常都是使能的,这句话也是使能的,一开始以为默认false,后来才知道,
//如果没有获取到这个配置,才会是false
boolean disableNetworkTime = SystemProperties.getBoolean("config.disable_networktime", false);
//这儿是将时间同步作为一个系统服务,system_server是一个进程,包括多个系统服务
if (!isWatch && !disableNetworkTime) {
t.traceBegin("StartNetworkTimeUpdateService");
try {
networkTimeUpdater = new NetworkTimeUpdateService(context);
ServiceManager.addService("network_time_update_service",
networkTimeUpdater);
} catch (Throwable e) {
reportWtf("starting NetworkTimeUpdate service", e);
}
}
//这边是启动时间同步服务
final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
networkTimeUpdaterF.systemRunning();
二、同步时间的定时器设置(重试逻辑)
源码:frameworks/base/services/core/java/com/android/server/NetworkTimeUpdateService.java
1、注册定时器广播接收器
//首先在这儿注册一个广播接收器,接收定时器发过来的所有时间同步定时广播
private void registerForAlarms() {
mContext.registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget();
}
}, new IntentFilter(ACTION_POLL));
}
//接收到定时器广播后,就处理定时器事件
private class MyHandler extends Handler {
public void handleMessage(Message msg) {
switch (msg.what) {
case EVENT_AUTO_TIME_ENABLED:
case EVENT_POLL_NETWORK_TIME:
case EVENT_NETWORK_CHANGED:
onPollNetworkTime(msg.what);
break;
}
}
}
2、时间同步间隔的设置
mPollingIntervalMs = mContext.getResources().getInteger(
com.android.internal.R.integer.config_ntpPollingInterval);
mPollingIntervalShorterMs = mContext.getResources().getInteger(
com.android.internal.R.integer.config_ntpPollingIntervalShorter);
mTryAgainTimesMax = mContext.getResources().getInteger(
com.android.internal.R.integer.config_ntpRetry);
com.android.internal.R.integer.config_ntpPollingInterval这个是在另一个资源文件里获取的,在frameworks/base/core/res/res/values/config.xml里
frameworks/base/core/res/res/values/config.xml
<!-- Remote server that can provide NTP responses. -->
<string translatable="false" name="config_ntpServer">ntp.aliyun.com</string>
<!-- Normal polling frequency in milliseconds -->
<integer name="config_ntpPollingInterval">300000</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>
<!-- Timeout to wait for NTP server response in milliseconds. -->
<integer name="config_ntpTimeout">5000</integer>
在这个config.xml文件里,
可以设置ntpserver,因为默认的地址在国内不能访问
config_ntpPollingInterval,这个配置是一个通用轮训间隔,默认是86400000,是24小时,我这儿改为了300秒暂时
config_ntpPollingIntervalShorter,这个是短时间轮训间隔
config_ntpRetry,这个是短时间轮训间隔的次数
3、时间同步时间间隔的逻辑
//NetworkTimeUpdateService构造函数定义了一个pollIntent,可以用于下面resetAlarm来设置定时器的发送广播的intent,这样在定时器时间来临时,定时器发送广播这边便可以知道已经到达定时时间
public NetworkTimeUpdateService(Context context) {
Intent pollIntent = new Intent(ACTION_POLL, null);
mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
}
/**
* Cancel old alarm and starts a new one for the specified interval.
* @param interval when to trigger the alarm, starting from now.
*/
private void resetAlarm(long interval) {
mAlarmManager.cancel(mPendingPollIntent);
long now = SystemClock.elapsedRealtime();
long next = now + interval;
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, next, mPendingPollIntent);
}
//这边的处理是短时间多次轮询和长时间轮询的处理
//刚开机时,由于网络较系统启动较慢,所以需要多次轮询,轮询间隔也会比较小
//所以这里mPollingIntervalShorterMs是短时间轮询,设置定时器就用resetAlarm方法就行
//每次接收到定时器广播后,这边的mTryAgainCounter会加1,当短时间轮询超过一定次数,默认为3,
//就会切换为长时间轮询mPollingIntervalMs
private void onPollNetworkTimeUnderWakeLock(int event) {
if (cachedNtpResult != null && cachedNtpResult.getAgeMillis() < mPollingIntervalMs) {
} else {
mTryAgainCounter++;
if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) {
resetAlarm(mPollingIntervalShorterMs);
} else {
// Try much later
mTryAgainCounter = 0;
resetAlarm(mPollingIntervalMs);
}
}
}
4、同步时间
当定时器到时间时,会去真正的NtpTrustedTime类里同步时间,mTime.forceRefresh() 会去进行时间同步
private final NtpTrustedTime mTime;
private void onPollNetworkTimeUnderWakeLock(int event) {
NtpTrustedTime.TimeResult cachedNtpResult = mTime.getCachedTimeResult();
if (cachedNtpResult == null || cachedNtpResult.getAgeMillis() >= mPollingIntervalMs) {
mTime.forceRefresh();
cachedNtpResult = mTime.getCachedTimeResult();
}
}
三、时间同步请求
1、获取ntp时间,并且设置系统时间
源码:frameworks/base/core/java/android/util/NtpTrustedTime.java
同步时间的操作在forceRefresh方法里,首先获取getNtpConnectionInfo(),获取相关的配置,
包括serverName和timeoutMillis,serverName默认是com.android,国内无法访问,我这里设
置为了ntp.aliyun.com,具体细节可以参照第二节第2小节。
然后是创建一个SntpClient,通过sntp获取到的时间,创建一个TimeResult对象,这个对象可以通过getCachedTimeResult()函数获取到
public TimeResult getCachedTimeResult() {
return mTimeResult;
}
frameworks/base/core/java/android/util/NtpTrustedTime.java
public boolean forceRefresh() {
NtpConnectionInfo connectionInfo = getNtpConnectionInfo();
final SntpClient client = new SntpClient();
final String serverName = connectionInfo.getServer();
final int timeoutMillis = connectionInfo.getTimeoutMillis();
Log.d(TAG, "forceRefresh, serverName = " + serverName);
if (client.requestTime(serverName, timeoutMillis, network)) {
long ntpCertainty = client.getRoundTripTime() / 2;
mTimeResult = new TimeResult(
client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty);
Log.d(TAG, "]forceRefresh()-->requestTime true");
return true;
} else {
Log.d(TAG, "forceRefresh()-->requestTime false");
return false;
}
}
2、根据拿到的ntp时间设置系统时间
再回到onPollNetworkTimeUnderWakeLock方法,mTime.forceRefresh后,我们看到
再次获取了一下cachedNtpResult,此时cachedNtpResult不再是null,所以下面代码的
if (cachedNtpResult != null && cachedNtpResult.getAgeMillis() < mPollingIntervalMs)
分支可以执行到,里面调用timeSuggestion将获取到的ntp时间设置到系统时间里
frameworks/base/services/core/java/com/android/server/NetworkTimeUpdateService.java
private void onPollNetworkTimeUnderWakeLock(int event) {
NtpTrustedTime.TimeResult cachedNtpResult = mTime.getCachedTimeResult();
if (cachedNtpResult == null || cachedNtpResult.getAgeMillis() >= mPollingIntervalMs) {
mTime.forceRefresh();
cachedNtpResult = mTime.getCachedTimeResult();
}
if (cachedNtpResult != null && cachedNtpResult.getAgeMillis() < mPollingIntervalMs) {
resetAlarm(mPollingIntervalMs);
// Suggest the time to the time detector. It may choose use it to set the system clock.
TimestampedValue<Long> timeSignal = new TimestampedValue<>(
cachedNtpResult.getElapsedRealtimeMillis(), cachedNtpResult.getTimeMillis());
NetworkTimeSuggestion timeSuggestion = new NetworkTimeSuggestion(timeSignal);
timeSuggestion.addDebugInfo("Origin: NetworkTimeUpdateService. event=" + event);
mTimeDetector.suggestNetworkTime(timeSuggestion);
} else {}
}
3、Sntp获取ntp服务器的时间
ntp端口123,在SntpClient里,会组一个udp包向ntpserver发送,并且阻塞的等待返回结果
public boolean requestTime(InetAddress address, int port, int timeout, Network network) {
DatagramSocket socket = null;
final int oldTag = TrafficStats.getAndSetThreadStatsTag(
TrafficStatsConstants.TAG_SYSTEM_NTP);
socket = new DatagramSocket();
network.bindSocket(socket);
socket.setSoTimeout(timeout);
byte[] buffer = new byte[NTP_PACKET_SIZE];
DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, port);
// set mode = 3 (client) and version = 3
// mode is in low 3 bits of first byte
// version is in bits 3-5 of first byte
buffer[0] = NTP_MODE_CLIENT | (NTP_VERSION << 3);
// get current time and write it to the request packet
final long requestTime = System.currentTimeMillis();
final long requestTicks = SystemClock.elapsedRealtime();
writeTimeStamp(buffer, TRANSMIT_TIME_OFFSET, requestTime);
socket.send(request);
// read the response
DatagramPacket response = new DatagramPacket(buffer, buffer.length);
socket.receive(response);
}
四、更改时区
Android11 如何修改默认时区_路过独木桥!!的博客-CSDN博客_android 设置默认时区
在build/make/core/main.mk文件里加一个属性
五、ntp原理
系统时钟同步的工作过程如下:
Device A发送一个NTP报文给Device B,该报文带有它离开Device A时的时间戳,该时间戳为10:00:00am(T1)。
当此NTP报文到达Device B时,Device B加上自己的时间戳,该时间戳为11:00:01am(T2)。
当此NTP报文离开Device B时,Device B再加上自己的时间戳,该时间戳为11:00:02am(T3)。
当Device A接收到该响应报文时,Device A的本地时间为10:00:03am(T4)。
至此,Device A已经拥有足够的信息来计算两个重要的参数:
NTP报文的往返时延Delay=(T4-T1)-(T3-T2)=2秒。
Device A相对Device B的时间差offset=((T2-T1)+(T3-T4))/2=1小时。
这样,Device A就能够根据这些信息来设定自己的时钟,使之与Device B的时钟同步。