32

I have created one application that replace the current showing time with server time. I have tried a lot, but I didn't find a solution. How can I set it to my device?

I know how to get the server time on my Android device. Is it possible in real time, specially on Android?

  • Server Time is automatically set to my device on my button click event.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
  • 2
    only system application can change your date and time of your device.your application can not have access to change DateTime of your Device. –  Aug 07 '13 at 11:46
  • possible duplicate of http://stackoverflow.com/q/2918624/752320 – Geobits Aug 07 '13 at 13:19
  • 1
    you can not change the system time programatically. SET_TIME si a permssion granted only to applications signed with the system signature – Blackbelt Aug 08 '13 at 09:50

4 Answers4

75

If you have the correct permission (see below), you can do this with the AlarmManager. For example, to set the time to 2013/08/15 12:34:56, you could do:

Calendar c = Calendar.getInstance();
c.set(2013, 8, 15, 12, 34, 56);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setTime(c.getTimeInMillis());

You need the permission SET_TIME to do this. Unfortunately, this is a signatureOrSystem permission.

Definition in AndroidManifest.xml:

    <!-- Allows applications to set the system time -->
    <permission android:name="android.permission.SET_TIME"
        android:protectionLevel="signature|system"
        android:label="@string/permlab_setTime"
        android:description="@string/permdesc_setTime" />

The only apps that can use this permission are:

  • Signed with the system image
  • Installed to the /system/ folder

Unless you build custom ROMs, you're out of luck with the first.

For the second, it depends on what you are doing.

  • If you're building an app for wide distribution (Google Play, etc.), you probably shouldn't. It's only an option for root users, and you'll only be able to install it manually. Any marketplace would not install it to the correct location.

  • If you're building an app for yourself (or just as a learning exercise), go for it. You'll need a rooted phone, though, so do that first. You can then install the application straight to /system/app/ with ADB or a file manager. See articles like this for more detail.


One final note: The SET_TIME permission and AlarmManager#setTime() were added in Android 2.2 (API 8). If you're trying to do this on a previous version, I'm not sure it will work at all.

Edit. use

<uses-permission> instead of <permission>
Jaswant Singh
  • 9,900
  • 8
  • 29
  • 50
Geobits
  • 22,218
  • 6
  • 59
  • 103
  • I Always get an Exception saying: "java.lang.SecurityException: setTime: Neither user 10041 nor current process has android.permission.SET_TIME." I set the permission: "android.permission.SET_TIME" in my Manifest. Can anybody confirm that? @Geobits – Sanket Shah Aug 08 '13 at 04:17
  • Is it installed as a system app? – Geobits Aug 08 '13 at 04:19
  • 12
    You will always get this security exception because, even though you requested `android.permission.SET_TIME` in your manifest, the system **will not grant your app this permission unless it is a system app**. Basically, in short, unless you have a rooted phone or your own custom ROM you cannot set the time on the device. Period. End of story. – David Wasser Aug 13 '13 at 17:59
  • @DavidWasser use – Jaswant Singh Jun 29 '20 at 15:03
  • @JaswantSingh Don't understand your edit, or your comment. In the answer the person (not me) showed the `` **definition** of the `SET_TIME` permission. He didn't say you should use that declaration. In my opinion, your edit is confusing and should be removed. In any case, even if you use `` you will still NOT be granted that permission unless your app is a system app. My comment stands. – David Wasser Jun 29 '20 at 15:29
  • I tried and it worked. You need to sign the application with the System (ROM) keys. – Jaswant Singh Jun 29 '20 at 16:14
12

Try this:

You can use the next command:

adb shell date -s YYYYMMDD.HHmmss

Example:

adb shell date -s 20120423.130000

sets the date to Mon Apr 23 13:00:00 IST 2012

Or you can also try this:

startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));

Hope it Helps!!

Sanchit
  • 315
  • 2
  • 20
Armaan Stranger
  • 3,140
  • 1
  • 14
  • 24
  • 3
    Note that the "adb" tool is not usable from the device to itself, at least not unless you have an unusual device, or have already used adb on a USB host to switch the device to from USB mode to TCP mode. While the commands used here with adb are shell commands, application code would not get to run them as a user with sufficient permission to work. – Chris Stratton Feb 10 '14 at 20:30
  • 1
    `date` seems to take different arguments now: `adb shell date MMDDhhmm[[CC]YY][.ss] set` sets the date to the given time. `adb shell date -h` shows full usage info. – Evan Jul 31 '17 at 17:00
6

Short Answer:

You cannot do it if you plan to have your app in the appstore, or update the time to the server time.

You need SignatureOrSystem permission in order to change the time date as per the server.

You might have to root your device and then implement the change.

This thread should help you a lot, to better understand it.

Community
  • 1
  • 1
JNL
  • 4,683
  • 18
  • 29
-3

I have done exactly the same for two weeks, now i got the solution with a rooted device

  1. Get server time

<?php echo date("Ymd.His "); ?>
  1. In your onClick method

    Process loProcess = Runtime.getRuntime().exec("su"); DataOutputStream loDataOutputStream = new DataOutputStream(loProcess.getOutputStream()); //loDataOutputStream.writeBytes("date -s 20120419.124012; \n"); loDataOutputStream.writeBytes("date -s" + theStringReturnedValue+ "; \n");

Note that theStringReturnedValue must to have this format 20150216.124012 which is the same that is returned in the php

Carlos Mtz
  • 151
  • 1
  • 1
  • 14
  • 2
    You failed to mention this requires a rooted device and root permissions for your app. Also you fail to close the `OutputStream`. – m0skit0 Nov 12 '18 at 10:54