快速修改android系统默认日期方法
在android系统的设备上,都有一个默认的开始日期,看过很多设备,有些设备在没有联网的时候没有同步到系统时间的时候,居然默认的还是1970年的日期,也见过有些设备默认到2000年1月1日的,这样相对进了一步,但是还不够。笔者下面很简单的介绍一下一个超级简单的方法:
/*****************************************************************************************************/
声明:本博内容均由http://blog.csdn.net/edsam49原创,转载请注明出处,谢谢!
/*****************************************************************************************************/
熟悉一下systemserver还是很好的,systemserver里面有好东西,首先还是从main进去,我们可以肯定原始的代码是这样写的:
public static void main(String[] args) {
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());
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); }
看了是不是感觉很觉得,改这个是简单,知道在这里可以改并不简单,加油!