修改Android系统默认时间

一 : 修改Android系统默认时间

源码路径:frameworks/base/services/java/com/android/server/SystemServer.java
主要变量EARLIEST_SUPPORTED_TIME

    // The earliest supported time.  We pick one day into 1970, to
    // give any timezone code room without going into negative time.
    private static final long EARLIEST_SUPPORTED_TIME = 1514764800000L;//20180101 00:00:00
  •  

通过SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME)设置系统时间,只需要修改EARLIEST_SUPPORTED_TIME变量的值。

    private void run() {
        try {
            traceBeginAndSlog("InitBeforeStartServices");
            // 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);
            }
  •  

设置系统默认时间这段代码请放在startOtherServices()之后,否则无效(不同的平台代码略有差别,可以自己评价该放在什么位置)。

        // Start services.
        try {
            traceBeginAndSlog("StartServices");
            startBootstrapServices();
            startCoreServices();
            startOtherServices();
            //add by jasun@180730 for default time start
            if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
                Slog.w(TAG, "System clock is before 2018.; setting to 2018.");
                SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
            }
            //add by jasun@180730 for default time end
            SystemServerInitThreadPool.shutdown();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        } finally {
            traceEnd();
        }
  •  

二 : 时间转换为UNIX时间戳

EARLIEST_SUPPORTED_TIME变量值如何获取?
Linux命令转换时间戳,如下:

$ date +%s --date 19700101 --utc   // 1970-01-01 00:00 UTC
0
$ date +%s --date 20120101 --utc   // 2012-01-01 00:00 UTC
1325376000
$ date +%s --date 20160101 --utc   // 2016-01-01 00:00 UTC
1451606400
  •  

反之,也可以把某个UNIX时间戳转换为具体日期,如下:

$ date --date=@86400 --utc // Android默认EARLIEST_SUPPORTED_TIME值为86400×1000
1970年 01月 02日 星期五 00:00:00 UTC
$ date --date=@1451606400 --utc
2016年 01月 01日 星期五 00:00:00 UTC
$ date --date=@1451606400
2016年 01月 01日 星期五 08:00:00 CST

--------------------- 作者:一莫言一 来源:CSDN 原文:https://blog.csdn.net/ljx646566715/article/details/81189914?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/u014630142/article/details/82994928
今日推荐