根植手机设置系统时间 [英] Setting system time of ROOTED phone

查看:211
本文介绍了根植手机设置系统时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在设置Android系统时间的软件。是的,我知道很多人尝试过它 - 没有像我现在要做的。 : - )

I am currently trying to set Android system time in software. Yes, I know that many people tried it - and failed like I do now. :-)

不过,我也知道这是可以设置Android系统的时间Root权限的手机。我已经testet称为时钟同步的应用程序,它不exaclty这一点。

But I also know that it is possible to set Android system time on ROOTED phones. I have testet an app called ClockSync which does exaclty that.

所以,我要的是找出如何Root权限的设备设置系统时间。请不要说这是不可能的。 : - )

So what I want is find out how to set system time on ROOTED devices. Please do not say it is not possible. :-)

我试过到目前为止是设置以下权限:

What I tried so far is setting the following permissions:

<uses-permission android:name="android.permission.SET_TIME_ZONE"/>
<uses-permission android:name="android.permission.SET_TIME"/>

然后在我的code:

And then in my code:

AlarmManager a = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
long current_time_millies = System.currentTimeMillis();
try {
    a.setTime((long)current_time_millies+10000);
} catch (Exception e) {
// Why is this exception thrown?
}

不过,我总是得到以下异常:

But I always get the following exception:

java.lang.SecurityException异常:的setTime:无论是用户还是10054当前进程android.permission.SET_TIME

java.lang.SecurityException: setTime: Neither user 10054 nor current process has android.permission.SET_TIME.

我在同一台设备,其中时钟同步完美的作品上测试它。所以 - 我究竟做错了什么?或者更好?你能提供测试code,它的工作原理

I am testing it on the same device where ClockSync works perfectly. So - what am I doing wrong? Or better: Can you provide tested code that works?

推荐答案

首先,我是的时钟同步,我知道一些关于Android上设定时间。

First of all, I'm the developer of ClockSync and I know something about setting time on Android.

恐怕以紫长颈鹿提供了答案是不正确的。问题是,普通用户应用程序不能访问的 SET_TIME 许可。此权限只能用于由安装作为ROM的部分或具有相同的密钥作为ROM本身(取决于供应商)的签名系统的应用程序。一个是使用这种方法的应用程序是 NTPC 。它与AOSP密钥签名,并可以在AOSP Android的基于ROM的,比如CyanogenMod的安装。请注意,谷歌已禁止AOSP键从市场最近,作者将永远无法更新了他的申请。 NTPC不能安装上大多数手机使用的常规光盘。

I'm afraid the answer provided by Violet Giraffe is not correct. The problem is that normal user application cannot gain access to SET_TIME permission. This permission can be used only by system applications that are installed as a part of the ROM or are signed with the same key as the ROM itself (depends on the vendor). One of the applications that is using this approach is NTPc. It's signed with the AOSP key and can be installed on the AOSP based Android ROMs, such as CyanogenMod. Note that Google has banned AOSP keys from Market recently and the author will never be able to update his application. NTPc cannot be installed on regular ROMs used on most of the phones.

如果您需要的详细信息,请务必阅读注释为著名的发行4581 允许用户应用程序来设置系统时间。请注意,这个问题是拒绝由谷歌与下面的注释:

If you need the details, make sure to read the comments for the famous issue 4581: Allow user apps to set the system time. Note that the issue was Declined by Google with the following comment:

它是由设计,应用程序不能更改时间。那里   是安全的许多细微的方面可以依靠当前的时间,   如证书过期,许可证管理等,我们不   要允许第三方应用程序在全球范围内破坏系统   以这种方式

Hi, it is by design that applications can not change the time. There are many subtle aspects of security that can rely on the current time, such as certificate expiration, license management, etc. We do not want to allow third party applications to globally disrupt the system in this way.

如何在植根设备上设置时间:

什么时钟同步不设置时间在变的<$ C许可$ C>的/ dev /报警设备。从本质上讲,它运行搭配chmod 666的/ dev /报警在root的shell。一旦该设备具有的所有用户的写权限, SystemClock。 <一个href="http://developer.android.com/reference/android/os/SystemClock.html#setCurrentTimeMillis%28long%29">setCurrentTimeMillis(...)电话会议将取得成功。一些应用程序使用另一种方法,他们跑日期命令,在root shell中有适当的参数,但它是容易出错,是不准确的,因为超级用户的shell和命令执行可能需要几秒钟。这种应用的一个例子是 Sytrant

What ClockSync does to set time is changing the permission of the /dev/alarm device. Essentially, it runs chmod 666 /dev/alarm in the root shell. Once this device has write permissions for all the users, SystemClock.setCurrentTimeMillis(...) call will succeed. Some applications use another approach, they run date command in the root shell with appropriate arguments, however it's error prone and is less accurate because superuser shell and command execution can take several seconds. An example of such application is Sytrant.

在默认情况下时钟同步设置666权限只有当的/ dev /报警已不写。这样可以节省CPU /电池因为SU / Superuser.apk执行是比较昂贵的。如果你担心安全问题,还有的恢复权限选项,这将使报警装置664的权限设定时间以后。

By default ClockSync sets 666 permission only if /dev/alarm is not already writable. This saves CPU/battery because su/Superuser.apk execution is relatively expensive. If you worry about security, there is Restore Permission option that will make the alarm device permission 664 after setting time.

有关从我用我自己的助手应用程序更容易root的shell访问:<一href="http://$c$c.google.com/p/market-enabler/source/browse/branches/MarketAccess/src/ru/org/amip/MarketAccess/utils/ShellInterface.java">ShellInterface.另一种选择是使用 RootTools 的库

For easier root shell access from the application I'm using my own helper class: ShellInterface. Another option is to use the RootTools library.

下面是根据<一个样品code href="http://$c$c.google.com/p/market-enabler/source/browse/branches/MarketAccess/src/ru/org/amip/MarketAccess/utils/ShellInterface.java">ShellInterface类:

  public void setTime(long time) {
    if (ShellInterface.isSuAvailable()) {
      ShellInterface.runCommand("chmod 666 /dev/alarm");
      SystemClock.setCurrentTimeMillis(time);
      ShellInterface.runCommand("chmod 664 /dev/alarm");
    }
  }

随意检查相关设定时间其他Android问题:

Feel free to check other Android issues related to setting time:

  • Issue 12497: ability for applications to gain permission to set time
  • Issue 18681: Implement timesync by NTP
  • Issue 67: Automatic setting of date and time using SIM or NTP

如果您需要在应用程序precise时间不使用的根和更改系统时间,你可以有你自己的定时器,例如基于乔达时间。您可以使用NTP客户端从阿帕奇公地网库。

If you need precise time in the application without using root and changing system time, you can have your own timers, for example based on Joda Time. You can get the time from NTP servers using NTP client from the Apache commons-net library.

这篇关于根植手机设置系统时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆