安卓10以后ntp的判定规则还有存放路径都不一样了,害我找了半天,现在做个汇总,如果大家只是简单的更改配置,就看下简单配置修改就好了。

简单配置

1.修改NTP基础配置

路径:/frameworks/base/core/res/res/values/config.xml

    <!-- Remote server that can provide NTP responses. -->
    <string translatable="false" name="config_ntpServer">2.android.pool.ntp.org</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">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">5</integer>
    <!-- Timeout to wait for NTP server response in milliseconds. -->
    <integer name="config_ntpTimeout">10</integer>

因客户需求改成如下配置:

依次对应

2.android.pool.ntp.org这个是NTP服务器地址

86400000是周期默认8640000ms,即1天,一般不改

30000是由于网络原因,时间同步失败后,retry的时间间隔,默认是60000ms,即60s我这里改成30s

5是retry的次数,默认是3次

10是时间误差,默认是5000ms,当时间误差超过5s,会更新系统时间,我改成10ms

2.通过adb更改查看当前服务器地址

adb shell settings put global ntp_server ntp.ntsc.ac.cn//更改服务器地址
adb shell setprop persist.sys.timezone Asia/Shangha//更改时区
adb shell settings get global ntp_server//查看当前使用地址
adb shel1 settings put global auto_time 1//打开网络同步时间开关
adb shel1 settings put global auto_time 0//关闭网络同步时间开关

3.踩过的坑

device\rockchip\rk356x\overlay\frameworks\base\core\res\res\values
在rk的源码里有许多配置被单独迁移出来,其中就包括ntp,在**device\rockchip**目录下有很多很多的配置文件如果你发现你改了配置后无效果,那么我建议你在这个目录下找找,会找到它的配置的。

修改NTP网络时间同步规则

1.修改类NetworkTimeUpdateService

路径:frameworks\base\services\core\java\com\android\server\NetworkTimeUpdateService
该类写了很多关于ntp的同步细则,流程等等,我们现在只分析这个判定规则onPollNetworkTimeUnderWakeLock
以下是我客制化后的配置:

    private void onPollNetworkTimeUnderWakeLock(int event) {
        // Force an NTP fix when outdated
        NtpTrustedTime.TimeResult cachedNtpResult = mTime.getCachedTimeResult();
        if (cachedNtpResult == null || cachedNtpResult.getAgeMillis() >= mPollingIntervalMs) {
            if (DBG) Log.d(TAG, "Stale NTP fix; forcing refresh");
            mTime.forceRefresh();
            cachedNtpResult = mTime.getCachedTimeResult();
        }

        if (cachedNtpResult != null && cachedNtpResult.getAgeMillis() < mPollingIntervalMs) {
            // Obtained fresh fix; schedule next normal update
            //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 {
            // No fresh fix; schedule retry
            mTryAgainCounter++;
            if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) {
                //resetAlarm(mPollingIntervalShorterMs);
            } else {
                // Try much later
                mTryAgainCounter = 0;
                //resetAlarm(mPollingIntervalMs);
            }
        }
    }

以为客户对于同步的误差要求很高,所以我直接注释了判定规则内的延时触发resetAlarm这个方法,如果还需要更精准,我建议直接注释判定规则,即时同步,不过肯定是会增加功耗的,以下是对该类的注解,帮助大家理解

   private void onPollNetworkTimeUnderWakeLock(int event) {
       // 使用NtpTrustedTime获取网络时间
       NtpTrustedTime.TimeResult cachedNtpResult = mTime.getCachedTimeResult();
       //cachedNtpResult.getAgeMillis()是上次请求ntp服务器的时间
       //如果大于等于1天,则强制刷新时间
       if (cachedNtpResult == null || cachedNtpResult.getAgeMillis() >= mPollingIntervalMs) {
           if (DBG) Log.d(TAG, "Stale NTP fix; forcing refresh");
           //该方法是个阻塞方法
           mTime.forceRefresh();
           cachedNtpResult = mTime.getCachedTimeResult();
       }
       //cachedNtpResult.getAgeMillis() < 1天
       if (cachedNtpResult != null && cachedNtpResult.getAgeMillis() < mPollingIntervalMs) {
           //设置定时广播,1天后触发
           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 {
           mTryAgainCounter++;
           if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) {
               //设置定时广播,10分钟后触发
               resetAlarm(mPollingIntervalShorterMs);
           } else {
               //设置定时广播,1天后触发
               mTryAgainCounter = 0;
               resetAlarm(mPollingIntervalMs);
           }
       }
   }

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐