安卓修改系统时间

2017-11-13  本文已影响45人  RunMoonlight

做安卓机顶盒开发的时候,由于盒子重启过后时间会重置,回到1970年,无法进行接口签名认证,所以需求就来了。

1、修改时间的核心代码如下:

```

static Process createSuProcess() throws IOException  {

File rootUser = new File("/system/xbin/ru");

if(rootUser.exists()) {

return Runtime.getRuntime().exec(rootUser.getAbsolutePath());

} else {

return Runtime.getRuntime().exec("su");

}

}

static Process createSuProcess(String cmd) throws IOException {

DataOutputStream os = null;

Process process = createSuProcess();

try {

os = new DataOutputStream(process.getOutputStream());

os.writeBytes(cmd + "\n");

os.writeBytes("exit $?\n");

} finally {

if(os != null) {

try {

os.close();

} catch (IOException e) {

}

}

}

return process;

}

static void requestPermission() throws InterruptedException, IOException {

createSuProcess("chmod 666 /dev/alarm").waitFor();

}

public void setDateTime(int year, int month, int day, int hour, int minute) {

try {

requestPermission();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Calendar c = Calendar.getInstance();

c.set(Calendar.YEAR, year);

c.set(Calendar.MONTH, month-1);

c.set(Calendar.DAY_OF_MONTH, day);

c.set(Calendar.HOUR_OF_DAY, hour);

c.set(Calendar.MINUTE, minute);

long when = c.getTimeInMillis();

if (when / 1000 < Integer.MAX_VALUE) {

SystemClock.setCurrentTimeMillis(when);

}

long now = Calendar.getInstance().getTimeInMillis();

Log.d(TAG, "set tm="+when + ", now tm="+now);

}

```

2、在应用程序的AndroidManifest.xml中的manifest节点中加入android:sharedUserId="android.uid.system"这个属性。

3、使用目标系统的platform密钥来重新给apk文件签名。将apk拷贝到当前目录下,然后执行Sign-platform.bat脚本,为apk进行系统签名。

执行完以上三步之后,采用普通安装方法,可以设置时间。

时间是可以设置了,但是签名也已经变了  不能更新升级。。。

上一篇 下一篇

猜你喜欢

热点阅读