您的位置:首页 > 移动开发 > Android开发

两种简单的方法修改Android系统下的系统默认时间

2017-01-17 20:02 501 查看
转载注明网址:http://blog.csdn.net/hubbybob1/article/details/54575572

前段时间在做Openssl的相关编程,发现一个错误:

error:14094412:SSL routines:SSL3_READ_BYTES:sslv3 alert bad certificate:s3_pkt.c:1259:SSL alert number 42
/*42:bad_certificate
There is a problem with the certificate, for example, a certificate is corrupt, or a certificate contains signatures that cannot be verified.*/


表示出现SSL错误码为42,经过查找列出了相关的错误码为bad_certificate,即CA证书出错,经过排查发现我的CA证书的时间是2016年制作,而我的Android平台系统时间却是1970年,所以会报错,那么就要修改Android平台的默认时间。

由于Android系统源码的默认时间为1970年,本文提供两种方法来修改Android的默认时间。

由于Android系统源码的默认时间为1970年,本文提供两种方法来修改Android的默认时间。

一、启动服务脚本来修改默认时间

修改系统时间的命令就是:

date -s //设置当前时间,只有root权限才能设置,其他只能查看
date -s 20170117//设置系统时间为2017年1月17日00:00:00
date -s 01:01:01 //设置具体时间,不会对日期做更改
date -s "01:01:01 2017-01-17" //这样可以设置全部时间
date -s "01:01:01 20170117" //这样可以设置全部时间
date -s "2017-01-17 01:01:01" //这样可以设置全部时间
date -s "20120117 01:01:01" //这样可以设置全部时间


1.编写脚本文件,随便命名为setdate.sh如下:

#!/system/bin/sh
su
date -s 20170117
date -s 12:00:00
echo "*******************************"
echo "set date 2017.01.17 12:00:00"
echo "*******************************"


把编写好的shell脚本放到固定的目录下面,例如可以放到和init.rc同一个目录下。

2.编写MK文件,找到你的启动mk文件,再起末尾添加如下copy语句:

PRODUCT_COPY_FILES += 相对本mk文件的存放脚本的路径/setdate.sh:system/bin/setdate.sh
//在运行过程中将setdate.sh脚本copy到/system/bin/下面


3.编写init.rc启动服务文件

在其最后添加以下内容,用来启动这个服务

service tiny /system/bin/setdate.sh
class main
oneshot
on property:sys.boot_completed=1
start setdate


到这里重新编译你的Android源码,安装就可以在启动系统的时候,使用date命令对系统的时间进行修改,其实这个不算严格意义上的修改默认的时间,下面的方法就是去Android源码里面去修改具体的时间了。

二、修改系统源码来解决默认时间

在这里主要讨论的是从Android系统源码出发来对系统时间的修改。其文件要找到是在

/frameworks/base/services/java/com/android/server/SystemServer.java


在这个文件中的第1180行的代码如下:

//在对象public class SystemServer内如下定义和判断
static Timer timer;//定义时间
static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr,一小时的毫秒数
//从970年1月一日0点0分0秒开始所经过的毫秒数
// 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 = 86400 * 1000;//可以查时间的ms数为1970的某天,定义最近时间

/**
* Called to initialize native system services.
*/
private static native void nativeInit();

public static void main(String[] args) {

/*
* In case the runtime switched since last boot (such as when
* the old runtime was removed in an OTA), set the system
* property so that it is in sync. We can't do this in
* libnativehelper's JniInvocation::Init code where we already
* had to fallback to a different runtime because it is
* running as root and we need to be the system user to set
* the property. http://b/11463182 */
SystemProperties.set("persist.sys.dalvik.vm.lib",
VMRuntime.getRuntime().vmLibrary());

if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {//获取当前时间和所设置的最近时间进行对比,,一般都是网络获取,没有网路的话一般都是1970年开始,可理解为没有互联网就设置为最近时间
// 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.
Slog.w(TAG, "System clock is before 1970; setting to 1970.");
SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);//设置为定义的最近时间,也就是默认时间
}


因此我们可以修改这个最近时间,也就是默认时间如下:

//private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;//可以查时间的ms数为1970的某天,
//private static final long EARLIEST_SUPPORTED_TIME = 1477899065689L;//设置时间为20140701
private static final long EARLIEST_SUPPORTED_TIME = 1482899065689L;//设置时间为2017年的某一天,某一时刻


到这里修改时间的两种方法已经完毕,是不是很简单啊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: