Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

android - Setting system time of ROOTED phone

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. :-)

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.

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"/>

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: Neither user 10054 nor current process has android.permission.SET_TIME.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATE: This method no longer works with the recent Android versions. The only other way that I'm aware of is to use the date command to set time. Note that command format may be different depending on the Android version and on the third-party tools installed (BusyBox version of date doesn't support time zones).

  // Android 6 and later default date format is "MMDDhhmm[[CC]YY][.ss]", that's (2 digits each)
  // month, day, hour (0-23), and minute. Optionally century, year, and second.
private static final SimpleDateFormat setDateFormat = new SimpleDateFormat("MMddHHmmyyyy.ss", Locale.US);

  // Standard Android date format: yyyymmdd.[[[hh]mm]ss]
  // http://stackoverflow.com/questions/5300999/set-the-date-from-a-shell-on-android
private static final SimpleDateFormat setDateFormat = new SimpleDateFormat("yyyyMMdd.HHmmss", Locale.US);

  // BusyBox date format:
  // [[[[[YY]YY]MM]DD]hh]mm[.ss]
  // but recent versions also accept MMDDhhmm[[YY]YY][.ss]
private static final SimpleDateFormat bbSetDateFormat = new SimpleDateFormat("yyyyMMddHHmm.ss", Locale.US);

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

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.

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.


How to set time on a rooted device:

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.

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.

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

Here is the sample code based on the ShellInterface class:

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

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


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...