安卓修改系统时间

做安卓机顶盒开发的时候,由于盒子重启过后时间会重置,回到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进行系统签名。

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


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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容