修改android系统时间出现的奇怪问题(已经有root权限)

新靖界 2014-06-24 03:45:09
我用的下面方法修改系统时间
try {
Process process = Runtime.getRuntime().exec("su");
String datetime = "20131023.112800"; // 测试的设置的时间【时间格式
// yyyyMMdd.HHmmss】
DataOutputStream os = new DataOutputStream(process
.getOutputStream());
os.writeBytes("setprop persist.sys.timezone GMT\n");
os.writeBytes("/system/bin/date -s "
+ datetime + "\n");
os.writeBytes("clock -w\n");
os.writeBytes("exit\n");
os.flush();
Toast.makeText(MainActivity.this, "系统时间同步成功",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(MainActivity.this,
"系统时间同步失败,莫非没有ROOT权限?", Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}

正常情况是时间被修改,修改后系统时间显示11:28,前天尝试成功了;可今天我用这段代码修改,系统时间却显示19:28,不知道是时区的问题还是什么情况,代码都没变。如果把os.writeBytes("setprop persist.sys.timezone GMT\n");中的GMT改为GMT+16:00后,再运行,系统时间修改成功,成11:28。但是昨天我也+16:00了,时间还是修改不对,也就是说有时修改的准,有时修改的不准,不知道为何,求大神指导!谢谢!
...全文
1886 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这个问题,困扰了我好几天 反对以上所有答案 这是google系统的一个bug,目前我的设备是 android 6.0 根据属性 presist.sys.timezone ,设置 GMT时间,在 bionic libc里面计算错误 源码位置bionic/libc/tzcode/localtime.c tzset_locked函数中, name = getenv("TZ"); if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0) { name = buf; } 当环境变量中 没有TZ的情况下,会从 persist.sys.timezone属性中拿。 但是接下来算时间的时候,会把GMT+ 算成GMT-(反之 GMT-会算成GMT+) 改起来也简单 if (name == NULL && __system_property_get("persist.sys.timezone", buf) > 0) { if(strlen(buf) > 3 && (strstr(buf,"GMT")-buf) == 0){ if(buf[3] == '-') buf[3] = '+'; else buf[3] = '-'; } name = buf; } 最新的android源码,已经修复了此问题,可以参考 https://github.com/android/platform_bionic/blob/master/libc/tzcode/localtime.c
新靖界 2016-09-24
  • 打赏
  • 举报
回复
app需要相应系统文件签名,不内置也可以
爱吃蛀牙 2016-06-08
  • 打赏
  • 举报
回复
SystemClock.setCurrentTimeMillis(when); 用的这个方法去设置系统时间.... 时间数据是服务器捕获....大部分的机器没有问题.但偶尔会有一两台..时间一直停留在11点多..什么鬼? 点开应用 重新测试一下又好了..... 后台常驻.,每5分钟一次同步
kkll1236 2016-05-18
  • 打赏
  • 举报
回复
引用 13 楼 lilidejing 的回复:
一定要内置,不然报异常java.lang.SecurityException: setTime: Neither user 10061 nor current process has android.permission.SET_TIME.
已经ROOT,APP放在 /system/app 里面 为什么在AndroidManifest.xml里最后加了这条权限,还是报错了,要怎么内置? <uses-permission android:name="android.permission.SET_TIME"/>
新靖界 2014-07-08
  • 打赏
  • 举报
回复
一定要内置,不然报异常java.lang.SecurityException: setTime: Neither user 10061 nor current process has android.permission.SET_TIME.
新靖界 2014-07-01
  • 打赏
  • 举报
回复
引用 10 楼 lilidejing 的回复:
解决了,加一条权限 <uses-permission android:name="android.permission.SET_TIME"/>
用的是楼上的方法
新靖界 2014-07-01
  • 打赏
  • 举报
回复
解决了,加一条权限 <uses-permission android:name="android.permission.SET_TIME"/>
箍心而行 2014-06-30
  • 打赏
  • 举报
回复
引用 7 楼 lilidejing 的回复:
[quote=引用 6 楼 a85454597 的回复:] Calendar c = Calendar.getInstance(); // 年 月-1 日 时 分 秒 if (date != null) { int year = date.getYear() + 1900; int day = date.getDate(); int month = date.getMonth(); int hour = date.getHours(); int minutes = date.getMinutes(); int seconds = date.getSeconds(); c.set(year, month, day, hour, minutes, seconds); } AlarmManager am = (AlarmManager) LoginActivity.this .getSystemService(Context.ALARM_SERVICE); am.setTime(c.getTimeInMillis()); 我是这么修改的 没有问题
你这个是得到系统时间么,系统时间不准怎么办[/quote] 我这个是从服务器获取时间 然后修改设备时间
新靖界 2014-06-30
  • 打赏
  • 举报
回复
引用 8 楼 a85454597 的回复:
[quote=引用 7 楼 lilidejing 的回复:] [quote=引用 6 楼 a85454597 的回复:] Calendar c = Calendar.getInstance(); // 年 月-1 日 时 分 秒 if (date != null) { int year = date.getYear() + 1900; int day = date.getDate(); int month = date.getMonth(); int hour = date.getHours(); int minutes = date.getMinutes(); int seconds = date.getSeconds(); c.set(year, month, day, hour, minutes, seconds); } AlarmManager am = (AlarmManager) LoginActivity.this .getSystemService(Context.ALARM_SERVICE); am.setTime(c.getTimeInMillis()); 我是这么修改的 没有问题
你这个是得到系统时间么,系统时间不准怎么办[/quote] 我这个是从服务器获取时间 然后修改设备时间[/quote] 执行这句代码am.setTime(c.getTimeInMillis());,报如下异常,有没遇到过呢? 06-29 19:32:35.703: E/AndroidRuntime(14289): java.lang.SecurityException: setTime: Neither user 10060 nor current process has android.permission.SET_TIME.
新靖界 2014-06-28
  • 打赏
  • 举报
回复
引用 6 楼 a85454597 的回复:
Calendar c = Calendar.getInstance(); // 年 月-1 日 时 分 秒 if (date != null) { int year = date.getYear() + 1900; int day = date.getDate(); int month = date.getMonth(); int hour = date.getHours(); int minutes = date.getMinutes(); int seconds = date.getSeconds(); c.set(year, month, day, hour, minutes, seconds); } AlarmManager am = (AlarmManager) LoginActivity.this .getSystemService(Context.ALARM_SERVICE); am.setTime(c.getTimeInMillis()); 我是这么修改的 没有问题
你这个是得到系统时间么,系统时间不准怎么办
箍心而行 2014-06-25
  • 打赏
  • 举报
回复
Calendar c = Calendar.getInstance(); // 年 月-1 日 时 分 秒 if (date != null) { int year = date.getYear() + 1900; int day = date.getDate(); int month = date.getMonth(); int hour = date.getHours(); int minutes = date.getMinutes(); int seconds = date.getSeconds(); c.set(year, month, day, hour, minutes, seconds); } AlarmManager am = (AlarmManager) LoginActivity.this .getSystemService(Context.ALARM_SERVICE); am.setTime(c.getTimeInMillis()); 我是这么修改的 没有问题
新靖界 2014-06-25
  • 打赏
  • 举报
回复
引用 4 楼 wyj23114 的回复:
请问lz如何获取的root权限?
系统本身自带root权限
伤痕累累 2014-06-25
  • 打赏
  • 举报
回复
请问lz如何获取的root权限?
新靖界 2014-06-25
  • 打赏
  • 举报
回复
没人知道吗
新靖界 2014-06-24
  • 打赏
  • 举报
回复
引用 1 楼 WUshuaijun1 的回复:
用真机事实没有 模拟器很多问题的有时候
就是用的真机,android电视盒
乐逍遥二 2014-06-24
  • 打赏
  • 举报
回复
用真机事实没有 模拟器很多问题的有时候

80,488

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧