Setting system time of ROOTED phone

后端 未结 2 1324
青春惊慌失措
青春惊慌失措 2020-11-29 02:39

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 possib

相关标签:
2条回答
  • 2020-11-29 03:02

    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();
            }
        } 
    
    0 讨论(0)
  • 2020-11-29 03:06

    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:

    • 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

    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.

    0 讨论(0)
提交回复
热议问题