Home ยป Android ยป Setting system time of ROOTED phone

Setting system time of ROOTED phone

Posted by: admin February 23, 2018 Leave a comment

Questions:

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?

Answers:

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.

Questions:
Answers:

you can set system Date and time on rooted device like this

public void setDate()
    {
        try {
            Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("date -s 20120419.024012; \n");
        } catch (Exception e) {
            Log.d(TAG,"error=="+e.toString());
            e.printStackTrace();
        }
    }