当前位置:   article > 正文

Android -- 系统网络时间更新NetworkTimeUpdateService服务解析_android更新系统时间意图

android更新系统时间意图

Android -- 系统网络时间更新NetworkTimeUpdateService服务解析


Android系统时间更新主要涉及到两种时间:NITZ时间和NTP时间。NITZ时间与手机服务运营商关系密切;而NTP网络时间则较为独立,访问某个服务器,获取时间即可。NTP时间获取的过程较为单一,且由于工作中主要是涉及到Android NTP时间的获取;所以这里先只介绍Android系统获取NTP时间的流程。


NTP其实是一种协议,Android系统中通过一个Socket向一个提供NTP服务的服务器发送请求来获取NTP时间。既然借助于Socket通信,那么功能实现的过程中就会涉及到服务器地址、请求超时时间等信息的配置。这部分我们在后续的分析过程中都会看到。


Android NTP时间更新主要由NetworkUpdateService服务处理,它决定何时更新NTP时间。在systemserver进程启动时,它会被初始化;同时,它是Binder的子类:

  1. networkTimeUpdater = new NetworkTimeUpdateService(context);
  2. ServiceManager.addService("network_time_update_service", networkTimeUpdater);
首先创建服务实例,接着将该服务发布到系统中。创建完服务对象,并当系统准备完成后,会直接启动该服务:
  1. try {
  2. if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemRunning();
  3. } catch (Throwable e) {
  4. reportWtf("Notifying NetworkTimeService running", e);
  5. }
手段就是调用NetworkTimeUpdateService::systemRunning()方法。通过这两步,时间更新服务就处于工作状态了。


那么我们通过分析这两个步骤,来理解NetworkTimeUpdateService的工作流程


一、创建NetworkTimeUpdateService


NetworkTimeUpdateService的构造函数实现如下:

  1. public NetworkTimeUpdateService(Context context) {
  2. mContext = context;
  3. mTime = NtpTrustedTime.getInstance(context);//初始化NtpTrustedTime实例,它负责NTP时间的获取
  4. mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);//获取闹钟服务
  5. //该PendingIntent对象用于当某次Ntp时间获取失败后,开启一个定时服务,去再次请求时间
  6. Intent pollIntent = new Intent(ACTION_POLL, null);
  7. mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
  8. //初始化了几个成员变量,它们决定了Ntp时间更新的细节
  9. mPollingIntervalMs = mContext.getResources().getInteger(
  10. com.android.internal.R.integer.config_ntpPollingInterval);
  11. mPollingIntervalShorterMs = mContext.getResources().getInteger(
  12. com.android.internal.R.integer.config_ntpPollingIntervalShorter);
  13. mTryAgainTimesMax = mContext.getResources().getInteger(
  14. com.android.internal.R.integer.config_ntpRetry);
  15. mTimeErrorThresholdMs = mContext.getResources().getInteger(
  16. com.android.internal.R.integer.config_ntpThreshold);
  17. mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).newWakeLock(
  18. PowerManager.PARTIAL_WAKE_LOCK, TAG);
  19. }
首先初始化NtpTrustedTime类实例,它负责NTP时间获取的具体操作,而NetworkTimeUpdateService服务决定何时触发NTP时间请求。


NtpTrustedTime实例的构造过程如下,它采用了单例设计模式:

  1. private NtpTrustedTime(String server, long timeout) {
  2. if (LOGD) Log.d(TAG, "creating NtpTrustedTime using " + server);
  3. mServer = server;
  4. mTimeout = timeout;
  5. }
  6. public static synchronized NtpTrustedTime getInstance(Context context) {
  7. if (sSingleton == null) {
  8. final Resources res = context.getResources();
  9. final ContentResolver resolver = context.getContentResolver();
  10. //读取用户配置的NTP服务器地址、超时信息等
  11. final String defaultServer = res.getString(
  12. com.android.internal.R.string.config_ntpServer);
  13. final long defaultTimeout = res.getInteger(
  14. com.android.internal.R.integer.config_ntpTimeout);
  15. final String secureServer = Settings.Global.getString(
  16. resolver, Settings.Global.NTP_SERVER);
  17. final long timeout = Settings.Global.getLong(
  18. resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout);
  19. final String server = secureServer != null ? secureServer : defaultServer;
  20. sSingleton = new NtpTrustedTime(server, timeout);
  21. sContext = context;
  22. }
  23. return sSingleton;
  24. }

读取配置文件和数据库配置的Ntp服务器地址和请求超时时间等信息,然后创建NtpTrustedTime对象并返回。其中,涉及到的服务器地址和超时信息会在config.xml和数据库中配置。如这里的默认NTP请求地址配置为:

  1. <!-- Remote server that can provide NTP responses. -->
  2. <string translatable="false" name="config_ntpServer">2.android.pool.ntp.org</string>
NtpTrustedTime封装了具体请求NTP时间的接口,NetworkTimeUpdateService如果需要更新NTP时间,通过创建的mTime实例即可实现。


回到NetworkTimeUpdateService的构造函数,创建完NtpTrustedTime实例后,还会获取Alarm服务对象;因为,当某刻NTP请求失败,系统会定时地去尝试再次获取NTP时间。其中创建的mPendingPollIntent就代表我们的这种意图。随后,这里初始化了几个long型变量,它会在NTP时间请求过程中决定一些细节分支处理,所以我们要明确它们的具体含义:

  1. //初始化了三个成员变量,它们决定了Ntp时间更新的细节
  2. mPollingIntervalMs = mContext.getResources().getInteger(
  3. com.android.internal.R.integer.config_ntpPollingInterval);//86400000ms
  4. mPollingIntervalShorterMs = mContext.getResources().getInteger(
  5. com.android.internal.R.integer.config_ntpPollingIntervalShorter);//60000ms
  6. mTryAgainTimesMax = mContext.getResources().getInteger(
  7. com.android.internal.R.integer.config_ntpRetry);//3
  8. mTimeErrorThresholdMs = mContext.getResources().getInteger(
  9. com.android.internal.R.integer.config_ntpThreshold);//5000

  • mPollingIntervalMs:Normal polling frequency in milliseconds -> 每两次刷新请求之间的时间间隔;决定是否需要刷新NTP时间
  • mPollingIntervalShorterMs:Try-again polling interval in milliseconds, in case the network request failed -> 某次请求失败后,再次去请求的时间间隔
  • mTryAgainTimesMax:Number of times to try again with the shorter interval, before backing off until the normal polling interval. A value < 0 indicates infinite -> 最大的尝试请求次数
  • mTimeErrorThresholdMs:If the time difference is greater than this threshold in milliseconds, then update the time -> 当新旧时间差别大于该值时,更新时间

初始化了这些成员变量,NetworkTimeUpdateService的创建工作就完成了。接着就要去启动该服务了。


二、NetworkTimeUpdateService::systemRunning()


NetworkTimeUpdateService实例创建完成后,会调用systemRunning()启动它:

  1. /** Initialize the receivers and initiate the first NTP request */
  2. public void systemRunning() {
  3. registerForTelephonyIntents();
  4. registerForAlarms();
  5. registerForConnectivityIntents();
  6. HandlerThread thread = new HandlerThread(TAG);
  7. thread.start();
  8. mHandler = new MyHandler(thread.getLooper());
  9. // Check the network time on the new thread
  10. mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget();
  11. mSettingsObserver = new SettingsObserver(mHandler, EVENT_AUTO_TIME_CHANGED);
  12. mSettingsObserver.observe(mContext);
  13. }
说是启动,其实主要的工作就是初始化了一些触发时间更新的条件。
  1. private void registerForTelephonyIntents() {
  2. IntentFilter intentFilter = new IntentFilter();
  3. intentFilter.addAction(TelephonyIntents.ACTION_NETWORK_SET_TIME);
  4. intentFilter.addAction(TelephonyIntents.ACTION_NETWORK_SET_TIMEZONE);
  5. mContext.registerReceiver(mNitzReceiver, intentFilter);
  6. }
  7. private void registerForAlarms() {
  8. mContext.registerReceiver(
  9. new BroadcastReceiver() {
  10. @Override
  11. public void onReceive(Context context, Intent intent) {
  12. mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget();
  13. }
  14. }, new IntentFilter(ACTION_POLL));
  15. }
  16. private void registerForConnectivityIntents() {
  17. IntentFilter intentFilter = new IntentFilter();
  18. intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
  19. mContext.registerReceiver(mConnectivityReceiver, intentFilter);
  20. }
  1. /** Receiver for Nitz time events */
  2. private BroadcastReceiver mNitzReceiver = new BroadcastReceiver() {
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. String action = intent.getAction();
  6. if (DBG) Log.d(TAG, "Received " + action);
  7. if (TelephonyIntents.ACTION_NETWORK_SET_TIME.equals(action)) {
  8. mNitzTimeSetTime = SystemClock.elapsedRealtime();
  9. } else if (TelephonyIntents.ACTION_NETWORK_SET_TIMEZONE.equals(action)) {
  10. mNitzZoneSetTime = SystemClock.elapsedRealtime();
  11. }
  12. }
  13. };
  14. /** Receiver for ConnectivityManager events */
  15. private BroadcastReceiver mConnectivityReceiver = new BroadcastReceiver() {
  16. @Override
  17. public void onReceive(Context context, Intent intent) {
  18. String action = intent.getAction();
  19. if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
  20. if (DBG) Log.d(TAG, "Received CONNECTIVITY_ACTION ");
  21. // Don't bother checking if we have connectivity, NtpTrustedTime does that for us.
  22. Message message = mHandler.obtainMessage(EVENT_NETWORK_CHANGED);
  23. // Send with a short delay to make sure the network is ready for use
  24. mHandler.sendMessageDelayed(message, NETWORK_CHANGE_EVENT_DELAY_MS);
  25. }
  26. }
  27. };

三个register方法分别注册了该服务关心的一些广播,其中一些广播的处理会通过发送消息事件去触发时间更新流程。其中,网络连接更新变化是会触发时间更新的;这在MyHandler的处理中可以看到。


MyHandler是NetworkTimeUpdateService内处理这些引发时间更新的异步消息事件的类,它工作在单独的线程中:

  1. /** Handler to do the network accesses on */
  2. private class MyHandler extends Handler {
  3. public MyHandler(Looper l) {
  4. super(l);
  5. }
  6. @Override
  7. public void handleMessage(Message msg) {
  8. switch (msg.what) {
  9. case EVENT_AUTO_TIME_CHANGED:
  10. case EVENT_POLL_NETWORK_TIME:
  11. case EVENT_NETWORK_CHANGED:
  12. onPollNetworkTime(msg.what);
  13. break;
  14. }
  15. }
  16. }
MyHandler中处理了三个事件,这样表明只在三种情况下,会主动去触发时间更新流程:

  • 监听到Settings.Global.AUTO_TIME字段有变化,表明用户是否使用自动时间
  • 服务内部开启的时间更新定时服务,如某次时间更新失败,需要在下一次再次去触发
  • 网络连接状态发生变化,如网络连接状态从未连接到连接,此时就需要触发时间更新

SettingsObserver会监听Settings.Global.AUTO_TIME字段是否变化,它对应的就是上面介绍的第一种触发情况:

  1. /** Observer to watch for changes to the AUTO_TIME setting */
  2. private static class SettingsObserver extends ContentObserver {
  3. private int mMsg;
  4. private Handler mHandler;
  5. SettingsObserver(Handler handler, int msg) {
  6. super(handler);
  7. mHandler = handler;
  8. mMsg = msg;
  9. }
  10. void observe(Context context) {
  11. ContentResolver resolver = context.getContentResolver();
  12. resolver.registerContentObserver(Settings.Global.getUriFor(Settings.Global.AUTO_TIME),
  13. false, this);
  14. }
  15. @Override
  16. public void onChange(boolean selfChange) {
  17. mHandler.obtainMessage(mMsg).sendToTarget();
  18. }
  19. }
当监听到Settings.Global.AUTO_TIME字段有变化时,会发送消息事件EVENT_AUTO_TIME_CHANGED给MyHandler;如果字段值设为了1,表明用户希望自动设置时间,那么此时就会去触发时间更新。

systemRunning()中还主动触发了一次时间更新操作:
  1. // Check the network time on the new thread
  2. mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget();
通过发送EVENT_POLL_NETWORK_TIME事件;三种情况下,时间更新的流程都由onPollNetworkTime()处理,分析一种情况就等于分析了三种情况。直接看它的处理过程:
  1. /**
  2. * Checks if the user prefers to automatically set the time.
  3. */
  4. private boolean isAutomaticTimeRequested() {
  5. return Settings.Global.getInt(
  6. mContext.getContentResolver(), Settings.Global.AUTO_TIME, 0) != 0;
  7. }
  8. private void onPollNetworkTime(int event) {
  9. // If Automatic time is not set, don't bother.
  10. if (!isAutomaticTimeRequested()) return;
  11. mWakeLock.acquire();
  12. try {
  13. onPollNetworkTimeUnderWakeLock(event);
  14. } finally {
  15. mWakeLock.release();
  16. }
  17. }
首先判断Settings.Global.AUTO_TIME字段的值,如果为1,则去自动更新、设置时间。onPollNetworkTimeUnderWakeLock()函数处理如下:
  1. private void onPollNetworkTimeUnderWakeLock(int event) {
  2. final long refTime = SystemClock.elapsedRealtime();//获取当前设备从开机算起的时间
  3. // If NITZ time was received less than mPollingIntervalMs time ago,
  4. // no need to sync to NTP.
  5. if (mNitzTimeSetTime != NOT_SET && refTime - mNitzTimeSetTime < mPollingIntervalMs) {//NITZ时间部分,如果两次时间的差异小于mPollingIntervalMs的值,则退出时间更新流程
  6. resetAlarm(mPollingIntervalMs);
  7. return;
  8. }
  9. final long currentTime = System.currentTimeMillis();//获取当前时间
  10. if (DBG) Log.d(TAG, "System time = " + currentTime);
  11. // Get the NTP time
  12. if (mLastNtpFetchTime == NOT_SET || refTime >= mLastNtpFetchTime + mPollingIntervalMs
  13. || event == EVENT_AUTO_TIME_CHANGED) {//如果我们是第一次设置时间,或者两次更新时间的间隔大于了mPollingIntervalMs值,或者当前触发更新的事件是EVENT_AUTO_TIME_CHANGED;则继续更新流程
  14. if (DBG) Log.d(TAG, "Before Ntp fetch");
  15. // force refresh NTP cache when outdated
  16. if (mTime.getCacheAge() >= mPollingIntervalMs) {//如果这次时间更新的时间节点和上一次时间更新时获取到结果的时间节点之间的差异大于mPollingIntervalMs时,认为时间过期,需要强制刷新时间;
  17. mTime.forceRefresh();
  18. }
  19. // only update when NTP time is fresh
  20. if (mTime.getCacheAge() < mPollingIntervalMs) {//这种情况下认为NTP时间是新鲜的、有效的
  21. final long ntp = mTime.currentTimeMillis();//获取当前的NTP时间
  22. mTryAgainCounter = 0;
  23. // If the clock is more than N seconds off or this is the first time it's been
  24. // fetched since boot, set the current time.
  25. if (Math.abs(ntp - currentTime) > mTimeErrorThresholdMs
  26. || mLastNtpFetchTime == NOT_SET) {
  27. // Set the system time
  28. if (DBG && mLastNtpFetchTime == NOT_SET
  29. && Math.abs(ntp - currentTime) <= mTimeErrorThresholdMs) {
  30. Log.d(TAG, "For initial setup, rtc = " + currentTime);
  31. }
  32. if (DBG) Log.d(TAG, "Ntp time to be set = " + ntp);
  33. // Make sure we don't overflow, since it's going to be converted to an int
  34. if (ntp / 1000 < Integer.MAX_VALUE) {
  35. SystemClock.setCurrentTimeMillis(ntp);//将NTP时间设置到内核中,此时时间才会生效
  36. }
  37. } else {
  38. if (DBG) Log.d(TAG, "Ntp time is close enough = " + ntp);
  39. }
  40. mLastNtpFetchTime = SystemClock.elapsedRealtime();
  41. } else {//否则,一段时间之后再去触发时间更新;根据当前的重试次数,时间间隔的设置也会变化
  42. // Try again shortly
  43. mTryAgainCounter++;
  44. if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) {
  45. //设置定时任务,隔一段时间后再去尝试刷新时间
  46. resetAlarm(mPollingIntervalShorterMs);
  47. } else {
  48. // Try much later
  49. mTryAgainCounter = 0;
  50. //设置定时任务,隔一段时间后再去尝试刷新时间
  51. resetAlarm(mPollingIntervalMs);
  52. }
  53. return;
  54. }
  55. }
  56. resetAlarm(mPollingIntervalMs);//设置定时任务,隔一段时间后再去尝试刷新时间
  57. }
NTP时间的刷新动作是由NtpTrustedTime::forceRefresh()函数实现:
  1. @Override
  2. public boolean forceRefresh() {
  3. if (TextUtils.isEmpty(mServer)) {
  4. // missing server, so no trusted time available
  5. return false;
  6. }
  7. // We can't do this at initialization time: ConnectivityService might not be running yet.
  8. synchronized (this) {
  9. if (mCM == null) {
  10. mCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
  11. }
  12. }
  13. final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo();
  14. if (ni == null || !ni.isConnected()) {
  15. if (LOGD) Log.d(TAG, "forceRefresh: no connectivity");
  16. return false;
  17. }
  18. if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
  19. final SntpClient client = new SntpClient();
  20. if (client.requestTime(mServer, (int) mTimeout)) {
  21. mHasCache = true;
  22. mCachedNtpTime = client.getNtpTime();
  23. mCachedNtpElapsedRealtime = client.getNtpTimeReference();
  24. mCachedNtpCertainty = client.getRoundTripTime() / 2;
  25. return true;
  26. } else {
  27. return false;
  28. }
  29. }

它内部借助SntpClient类来实现此时的NTP socket请求:

  1. final SntpClient client = new SntpClient();
  2. if (client.requestTime(mServer, (int) mTimeout)) {
  3. mHasCache = true;
  4. mCachedNtpTime = client.getNtpTime();
  5. mCachedNtpElapsedRealtime = client.getNtpTimeReference();
  6. mCachedNtpCertainty = client.getRoundTripTime() / 2;
  7. return true;
  8. } else {
  9. return false;
  10. }

SntpClient封装了获取NTP时间的socket接口:

  1. /**
  2. * Sends an SNTP request to the given host and processes the response.
  3. *
  4. * @param host host name of the server.
  5. * @param timeout network timeout in milliseconds.
  6. * @return true if the transaction was successful.
  7. */
  8. public boolean requestTime(String host, int timeout) {
  9. InetAddress address = null;
  10. try {
  11. address = InetAddress.getByName(host);//
  12. } catch (Exception e) {
  13. if (DBG) Log.d(TAG, "request time failed: " + e);
  14. return false;
  15. }
  16. return requestTime(address, NTP_PORT, timeout);
  17. }
  18. public boolean requestTime(InetAddress address, int port, int timeout) {
  19. DatagramSocket socket = null;
  20. try {
  21. socket = new DatagramSocket();
  22. socket.setSoTimeout(timeout);
  23. byte[] buffer = new byte[NTP_PACKET_SIZE];
  24. DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, port);
  25. // set mode = 3 (client) and version = 3
  26. // mode is in low 3 bits of first byte
  27. // version is in bits 3-5 of first byte
  28. buffer[0] = NTP_MODE_CLIENT | (NTP_VERSION << 3);
  29. // get current time and write it to the request packet
  30. final long requestTime = System.currentTimeMillis();//开始请求的时间节点记录,当前时间
  31. final long requestTicks = SystemClock.elapsedRealtime();//开始请求的时间时钟记录,从开机算起
  32. writeTimeStamp(buffer, TRANSMIT_TIME_OFFSET, requestTime);
  33. socket.send(request);//开始发送请求
  34. // read the response
  35. DatagramPacket response = new DatagramPacket(buffer, buffer.length);
  36. socket.receive(response);//开始读取响应
  37. final long responseTicks = SystemClock.elapsedRealtime();//读取响应的时间时钟记录,从开机算起
  38. final long responseTime = requestTime + (responseTicks - requestTicks);//响应时间 = 开始发送请求的时间节点 + (响应时钟节点 - 请求时钟节点)
  39. // extract the results
  40. final byte leap = (byte) ((buffer[0] >> 6) & 0x3);
  41. final byte mode = (byte) (buffer[0] & 0x7);
  42. final int stratum = (int) (buffer[1] & 0xff);
  43. final long originateTime = readTimeStamp(buffer, ORIGINATE_TIME_OFFSET);
  44. final long receiveTime = readTimeStamp(buffer, RECEIVE_TIME_OFFSET);
  45. final long transmitTime = readTimeStamp(buffer, TRANSMIT_TIME_OFFSET);
  46. /* do sanity check according to RFC */
  47. // TODO: validate originateTime == requestTime.
  48. checkValidServerReply(leap, mode, stratum, transmitTime);
  49. long roundTripTime = responseTicks - requestTicks - (transmitTime - receiveTime);
  50. // receiveTime = originateTime + transit + skew
  51. // responseTime = transmitTime + transit - skew
  52. // clockOffset = ((receiveTime - originateTime) + (transmitTime - responseTime))/2
  53. // = ((originateTime + transit + skew - originateTime) +
  54. // (transmitTime - (transmitTime + transit - skew)))/2
  55. // = ((transit + skew) + (transmitTime - transmitTime - transit + skew))/2
  56. // = (transit + skew - transit + skew)/2
  57. // = (2 * skew)/2 = skew
  58. long clockOffset = ((receiveTime - originateTime) + (transmitTime - responseTime))/2;
  59. if (DBG) {
  60. Log.d(TAG, "round trip: " + roundTripTime + "ms, " +
  61. "clock offset: " + clockOffset + "ms");
  62. }
  63. // save our results - use the times on this side of the network latency
  64. // (response rather than request time)
  65. mNtpTime = responseTime + clockOffset;//计算得到的ntp时间
  66. mNtpTimeReference = responseTicks;//读取响应的时间节点
  67. mRoundTripTime = roundTripTime;
  68. } catch (Exception e) {
  69. if (DBG) Log.d(TAG, "request time failed: " + e);
  70. return false;
  71. } finally {
  72. if (socket != null) {
  73. socket.close();
  74. }
  75. }
  76. return true;
  77. }
一个标准的socket发送请求-读取响应的模式。实现中,记录了一些特殊的时间节点,我们需要注意。

系统是如何判定当前NTP时间是否需要刷新呢?

  1. // force refresh NTP cache when outdated
  2. if (mTime.getCacheAge() >= mPollingIntervalMs) {//如果这次时间更新的时间节点和上一次时间更新时获取到结果的时间节点之间的差异大于mPollingIntervalMs时,认为时间过期,需要强制刷新时间;
  3. mTime.forceRefresh();
  4. }
  1. @Override
  2. public long getCacheAge() {
  3. if (mHasCache) {
  4. return SystemClock.elapsedRealtime() - mCachedNtpElapsedRealtime;//mCachedNtpElapsedRealtime = client.getNtpTimeReference();
  5. } else {
  6. return Long.MAX_VALUE;
  7. }
  8. }
  1. /**
  2. * Returns the reference clock value (value of SystemClock.elapsedRealtime())
  3. * corresponding to the NTP time.
  4. *
  5. * @return reference clock corresponding to the NTP time.
  6. */
  7. public long getNtpTimeReference() {
  8. return mNtpTimeReference;
  9. }

具体的信息可以参考里面的注释。NTP时间刷新完成后,我们还需要将它设置到系统中,其实也就是设置到kernel中:

  1. // only update when NTP time is fresh
  2. if (mTime.getCacheAge() < mPollingIntervalMs) {//这种情况下认为NTP时间是新鲜的、有效的
  3. final long ntp = mTime.currentTimeMillis();//获取当前的NTP时间
  4. mTryAgainCounter = 0;
  5. // If the clock is more than N seconds off or this is the first time it's been
  6. // fetched since boot, set the current time.
  7. if (Math.abs(ntp - currentTime) > mTimeErrorThresholdMs
  8. || mLastNtpFetchTime == NOT_SET) {
  9. // Set the system time
  10. if (DBG && mLastNtpFetchTime == NOT_SET
  11. && Math.abs(ntp - currentTime) <= mTimeErrorThresholdMs) {
  12. Log.d(TAG, "For initial setup, rtc = " + currentTime);
  13. }
  14. if (DBG) Log.d(TAG, "Ntp time to be set = " + ntp);
  15. // Make sure we don't overflow, since it's going to be converted to an int
  16. if (ntp / 1000 < Integer.MAX_VALUE) {
  17. SystemClock.setCurrentTimeMillis(ntp);//将NTP时间设置到内核中,此时时间才会生效
  18. }
  19. } else {
  20. if (DBG) Log.d(TAG, "Ntp time is close enough = " + ntp);
  21. }
  22. mLastNtpFetchTime = SystemClock.elapsedRealtime();
  23. }

判断获取到的NTP时间的有效性,随后SystemClock.setCurrentTimeMillis()函数会将时间设置到内核中,让其生效;这样我们的时间就得到更新了。

如果某次的时间刷新操作失败了,系统会设置定时任务,在某个时间段之后再去触发时间刷新:

  1. * Cancel old alarm and starts a new one for the specified interval.
  2. *
  3. * @param interval when to trigger the alarm, starting from now.
  4. */
  5. private void resetAlarm(long interval) {
  6. mAlarmManager.cancel(mPendingPollIntent);
  7. long now = SystemClock.elapsedRealtime();
  8. long next = now + interval;
  9. mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, next, mPendingPollIntent);
  10. }
定时器任务触发时,会引起如下处理:
  1. //用于定时器
  2. private void registerForAlarms() {
  3. mContext.registerReceiver(
  4. new BroadcastReceiver() {
  5. @Override
  6. public void onReceive(Context context, Intent intent) {
  7. mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget();
  8. }
  9. }, new IntentFilter(ACTION_POLL));
  10. }
向MyHandler发送EVENT_POLL_NETWORK_TIME事件,再次去调用onPollNetworkTime()函数处理当前的时间刷新请求,重复之前的分析流程。


NetworkTimeUpdateService服务启动及NTP时间刷新流程就分析完成了。





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

闽ICP备14008679号