android9.0 系统默认时间修改

一. 问题:最近项目出现未联网的机器默认时间从2009-01-01 00:00 开始计时,桌面显示2009-01-01 03:00,需求是2019-01-01 8:00 开始计时

二.问题分析:

1. 通过分析代码,frameworks\base\services\java\com\android\server\SystemServer.java 

  private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;

 private void run()中

// If a device's clock is before 1970 (before 0), a lot of
        // APIs crash dealing with negative numbers, notably
        // java.io.File#setLastModified, so instead we fake it and
        // hope that time from cell towers or NTP fixes it shortly.
        if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
            Slog.w(TAG, "System clock is before 1970; setting to 1970.");
            SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
        }

        // Here we go!
        Slog.i(TAG, "Entered the Android system server!");
        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, SystemClock.uptimeMillis());

此处判断的是1970年

2. 抓取log:

01-01 08:04:37.768  3524  3524 D SystemServerTiming: StartConsumerIrService took to complete: 3ms
01-01 08:04:37.769  3524  3524 I SystemServer: StartAlarmManagerService
01-01 08:04:37.769  3524  3524 I SystemServiceManager: Starting com.android.server.AlarmManagerService
01-01 08:04:37.773  3524  3524 I AlarmManager: Current time only 277773, advancing to build time 1230739200000
01-01 00:00:00.008  3524  3524 D SystemServerTiming: StartAlarmManagerService took to complete: 13ms
01-01 00:00:00.008  3524  3524 I SystemServer: InitWatchdog
01-01 00:00:00.009  3524  3524 D SystemServerTiming: InitWatchdog took to complete: 0ms
01-01 00:00:00.009  3524  3524 I SystemServer: StartInputManagerService
01-01 00:00:00.010  3524  3524 I InputManager: Initializing input manager, mUseDevInputEventForAudioJack=false
01-01 00:00:00.011  3524  3524 D SystemServerTiming: StartInputManagerService took to complete: 2ms
01-01 00:00:00.011  3524  3524 I SystemServer: StartWindowManagerService

追查源码:frameworks\base\services\core\java\com\android\server\AlarmManagerService.java

发现以下设置,对比当前系统时间和system 文件夹时间

// We have to set current TimeZone info to kernel
        // because kernel doesn't keep this after reboot
        setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY));

        // Also sure that we're booting with a halfway sensible current time
        if (mNativeData != 0) {
            final long systemBuildTime = Environment.getRootDirectory().lastModified();
            if (System.currentTimeMillis() < systemBuildTime) {
                Slog.i(TAG, "Current time only " + System.currentTimeMillis()
                        + ", advancing to build time " + systemBuildTime);
                setKernelTime(mNativeData, systemBuildTime);
            }
        }

发现系统时间早于文件夹时间,将系统时间设置为文件夹时间

drwxr-xr-x  14 root   root      4096 2009-01-01 00:00 system
-rw-r--r--   1 root   root      5272 2009-01-01 00:00 ueventd.rc
drwxr-xr-x  13 root   root      4096 2009-01-01 00:00 vendor

三.解决方案:

修改默认时间

方法1, 在原有的时间设置 frameworks\base\services\java\com\android\server\SystemServer.java

if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
            Slog.w(TAG, "System clock is before 1970; setting to 1970.");
            SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
        }

设置:EARLIEST_SUPPORTED_TIME=1546300800000L,发现不起作用,需要在以下代码出设置:

private static final long EARLIEST_SUPPORTED_TIME_DH= 1546300800000L;

// Start services.
        try {
            traceBeginAndSlog("StartServices");
            startBootstrapServices();
            startCoreServices();
            startOtherServices();
            //begin 201901010800
            if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME_DH) {
                Slog.w(TAG, "Start services System clock is before 201901010800; setting to 201901010800.");
                SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME_HR);
            }
            //end
            SystemServerInitThreadPool.shutdown();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            traceEnd();
        }

方法二 frameworks\base\services\core\java\com\android\server\AlarmManagerService.java

private static final long DEFAULT_SUPPORTED_TIME_DH= 1546300800000L;

public void onStart() 方法中:

// We have to set current TimeZone info to kernel
        // because kernel doesn't keep this after reboot
        setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY));

//begin 201901010800

if (mNativeData != 0) {

  if (System.currentTimeMillis() < DEFAULT_SUPPORTED_TIME_DH) {
                Slog.i(TAG, "Current time only " + System.currentTimeMillis()
                        + ", advancing to time " + DEFAULT_SUPPORTED_TIME_DH);
                setKernelTime(mNativeData, DEFAULT_SUPPORTED_TIME_DH);
            }

}

 

//end 201901010800

        // Also sure that we're booting with a halfway sensible current time
        if (mNativeData != 0) {
            final long systemBuildTime = Environment.getRootDirectory().lastModified();
            if (System.currentTimeMillis() < systemBuildTime) {
                Slog.i(TAG, "Current time only " + System.currentTimeMillis()
                        + ", advancing to build time " + systemBuildTime);
                setKernelTime(mNativeData, systemBuildTime);
            }
        }

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值