Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>UPDATE</strong>: This method no longer works with the recent Android versions. The only other way that I'm aware of is to use the <code>date</code> 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 <code>date</code> doesn't support time zones).</p> <pre><code> // 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); </code></pre> <hr> <p>First of all, I'm the developer of <a href="https://market.android.com/details?id=ru.org.amip.ClockSync" rel="nofollow noreferrer">ClockSync</a> and I know something about setting time on Android.</p> <p>I'm afraid the answer provided by <a href="https://stackoverflow.com/users/634821/violet-giraffe">Violet Giraffe</a> is not correct. The problem is that normal user application cannot gain access to <strong>SET_TIME</strong> 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 <a href="http://www.appbrain.com/app/ntpc/jp.pericia.timesync" rel="nofollow noreferrer">NTPc</a>. 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.</p> <p>If you need the details, make sure to read the comments for the famous <strong>issue 4581</strong>: <a href="http://code.google.com/p/android/issues/detail?id=4581" rel="nofollow noreferrer">Allow user apps to set the system time</a>. Note that the issue was <strong>Declined</strong> by Google with the following comment:</p> <blockquote> <p>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.</p> </blockquote> <hr> <p><strong>How to set time on a rooted device:</strong></p> <p>What <a href="https://market.android.com/details?id=ru.org.amip.ClockSync" rel="nofollow noreferrer">ClockSync</a> does to set time is changing the permission of the <code>/dev/alarm</code> device. Essentially, it runs <code>chmod 666 /dev/alarm</code> in the root shell. Once this device has write permissions for all the users, <code>SystemClock.</code><a href="http://developer.android.com/reference/android/os/SystemClock.html#setCurrentTimeMillis%28long%29" rel="nofollow noreferrer">setCurrentTimeMillis(...)</a> call will succeed. Some applications use another approach, they run <code>date</code> 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 <a href="http://www.appbrain.com/app/sytrant/ru.atrant.sytrant" rel="nofollow noreferrer">Sytrant</a>.</p> <p>By default ClockSync sets 666 permission only if <code>/dev/alarm</code> is not already writable. This saves CPU/battery because su/Superuser.apk execution is relatively expensive. If you worry about security, there is <strong>Restore Permission</strong> option that will make the alarm device permission 664 after setting time.</p> <p>For easier root shell access from the application I'm using my own helper class: <a href="http://code.google.com/p/market-enabler/source/browse/branches/MarketAccess/src/ru/org/amip/MarketAccess/utils/ShellInterface.java" rel="nofollow noreferrer">ShellInterface</a>. Another option is to use the <a href="http://code.google.com/p/roottools/" rel="nofollow noreferrer">RootTools</a> library.</p> <p>Here is the sample code based on the <a href="http://code.google.com/p/market-enabler/source/browse/branches/MarketAccess/src/ru/org/amip/MarketAccess/utils/ShellInterface.java" rel="nofollow noreferrer">ShellInterface</a> class:</p> <pre><code> public void setTime(long time) { if (ShellInterface.isSuAvailable()) { ShellInterface.runCommand("chmod 666 /dev/alarm"); SystemClock.setCurrentTimeMillis(time); ShellInterface.runCommand("chmod 664 /dev/alarm"); } } </code></pre> <p>Feel free to check other Android issues related to setting time:</p> <ul> <li>Issue 12497: <a href="http://code.google.com/p/android/issues/detail?id=12497" rel="nofollow noreferrer">ability for applications to gain permission to set time</a></li> <li>Issue 18681: <a href="http://code.google.com/p/android/issues/detail?id=18681" rel="nofollow noreferrer">Implement timesync by NTP</a></li> <li>Issue 67: <a href="http://code.google.com/p/android-on-freerunner/issues/detail?id=67" rel="nofollow noreferrer">Automatic setting of date and time using SIM or NTP</a></li> </ul> <hr> <p>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 <a href="http://joda.org" rel="nofollow noreferrer">Joda Time</a>. You can get the time from NTP servers using NTP client from the <a href="http://commons.apache.org/net/" rel="nofollow noreferrer">Apache commons-net</a> library.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload