標籤:android style blog http java color
快速修改android系統預設日期方法
在android系統的裝置上,都有一個預設的開始日期,看過很多裝置,有些裝置在沒有連網的時候沒有同步到系統時間的時候,居然預設的還是1970年的日期,也見過有些裝置預設到2000年1月1日的,這樣相對進了一步,但是還不夠。筆者下面很簡單的介紹一下一個超級簡單的方法:
/*****************************************************************************************************/
聲明:本博內容均由http://blog.csdn.net/edsam49原創,轉載請註明出處,謝謝!
/*****************************************************************************************************/
熟悉一下systemserver還是很好的,systemserver裡面有好東西,首先還是從main進去,我們可以肯定原始的代碼是這樣寫的:
public static void main(String[] args) {
1141
1142 /*
1143 * In case the runtime switched since last boot (such as when
1144 * the old runtime was removed in an OTA), set the system
1145 * property so that it is in sync. We can‘t do this in
1146 * libnativehelper‘s JniInvocation::Init code where we already
1147 * had to fallback to a different runtime because it is
1148 * running as root and we need to be the system user to set
1149 * the property. http://b/11463182
1150 */
1151 SystemProperties.set("persist.sys.dalvik.vm.lib",
1152 VMRuntime.getRuntime().vmLibrary());
1153
1154 if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
1155 // If a device‘s clock is before 1970 (before 0), a lot of
1156 // APIs crash dealing with negative numbers, notably
1157 // java.io.File#setLastModified, so instead we fake it and
1158 // hope that time from cell towers or NTP fixes it
1159 // shortly.
1160 Slog.w(TAG, "System clock is before 1970; setting to 1970.");
1161 SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
1162 }
明顯裡面有一個判斷當然時間,跟預設時間點的一個比較,如果比預設時間點晚的話,就設定成這個時間點,充分利用這一點就很容易了。還是用這種方法,只不過把預設的時間點挪動一下,實際上只要改一行不是代碼的代碼就可以了,筆者修改如下:
- private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;-+ //private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;+ //default 2014-07-01-12:00+ private static final long EARLIEST_SUPPORTED_TIME = 1404187200000L;+ /** * Called to initialize native system services. */@@ -1157,7 +1159,8 @@ public class SystemServer { // java.io.File#setLastModified, so instead we fake it and // hope that time from cell towers or NTP fixes it // shortly.- Slog.w(TAG, "System clock is before 1970; setting to 1970.");+ //Slog.w(TAG, "System clock is before 1970; setting to 1970.");+ Slog.w(TAG, "System clock is before 20140701; setting to 20140701."); SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME); }
看了是不是感覺很覺得,改這個是簡單,知道在這裡可以改並不簡單,加油!