1. 程式人生 > >修改Android系統預設時間

修改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 
[email protected]
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 [email protected] 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 [email protected] --utc // Android預設EARLIEST_SUPPORTED_TIME值為86400×1000
1970年 01月 02日 星期五 00:00:00 UTC
$ date [email protected] --utc
2016年 01月 01日 星期五 00:00:00 UTC
$ date [email protected]
2016年 01月 01日 星期五 08:00:00 CST

--------------------- 作者:一莫言一 來源:CSDN 原文:https://blog.csdn.net/ljx646566715/article/details/81189914?utm_source=copy 版權宣告:本文為博主原創文章,轉載請附上博文連結!