6

I trying some stuffs with android as i am learning android development, now i have a scenario here.

  • I want to change the android phone's system date through my application( first i would like to know is this doable? )

Layout is as below

layout

Now what i want is when the user clicks the button the date should increase by say 20 days how can i do this.

i am unable to start...please help

Community
  • 1
  • 1
karan
  • 8,637
  • 3
  • 41
  • 78
  • What have you tried and what you won't unable to start? Can you provide more information? – JustWork Jun 05 '13 at 19:35
  • unable to start in the sense i don't know, i don't know how can i get the system date and change it through my application. what inbuilt classes provides the functionality to get and set system dates – karan Jun 05 '13 at 19:41
  • you want to change the mobile date when your press on the button? – Blackbelt Jun 07 '13 at 20:31
  • yeah,it is what i want – karan Jun 07 '13 at 20:34
  • 1
    that`s impossible. You need the SET_TIME permission and that permission is granted by system only to applications that are in the Android system image. If You are able to gain that privilege you can easily change with the AlarmManager – Blackbelt Jun 07 '13 at 20:37
  • if the phone is rooted then? – karan Jun 07 '13 at 20:38
  • if it is rooted you can change the permission of /dev/alarm and use SystemClock.setCurrentTimeMillis(long). The better choice is to start startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS)); and let the user change it – Blackbelt Jun 07 '13 at 20:46
  • 1
    Some more interesting stuff about this issue to read: http://stackoverflow.com/questions/1332269/how-to-set-mobile-system-time-and-date-in-android ;) – Trinimon Jun 07 '13 at 20:47
  • a piece of code or classes involved would be helpful – karan Jun 07 '13 at 20:48
  • Runtime.getRuntime().exec("su"); to became root Runtime.getRuntime().exec("chmod 666 /dev/alarm"); – Blackbelt Jun 07 '13 at 20:50
  • any fortune with your issue? – Blackbelt Jun 12 '13 at 08:30
  • Why would you do this? Do you need to sync the device's time with server time so you will avoid any response errors caused by "request in the past/future"? – gunar Jun 13 '13 at 06:46

4 Answers4

8

As I already said that's impossible. You need the SET_TIME permission and that permission is granted by system only to applications that are in the Android system image. If You are able to gain that privilege you can easily change with the AlarmManager. SystemClock.setCurrentTimeMillis write the /dev/allarm file

adb shell ls -l /dev/alarm
crw-rw-r-- system   radio     10,  46 2013-06-12 10:46 alarm
  • c stays for Character special file (stored in /dev).
  • system is the owner of the file
  • radio is the group

system and radio have read and write permissions (rw-, tree bits, int value 6), the others have only the read permission (r, int value 4). So the file permission is 664. If you can get root user (running su), you can change the permission of this file and wrote in it a new value. A bug report has been filled in order to ask google to allow apps to modify programmatically the mobile date but it has been declied. Here the reference

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
4

On Android, the only way for an application do have write access to the time&date is to get the SET_TIME permission, which is only possible for "applications that are in the Android system image or that are signed with the same certificate as the application that declared the permission." (see signatureOrSystem protection level).

The only way for your application to reach this protection level is to run on a rooted device, or build and sign your own android rom.

If this is your case, you can easily use the AlarmManager or simply the Calendar instance.

Good luck!

Nicolas Floquet
  • 424
  • 3
  • 11
  • I think there is more to it than just running on a rooted device. I'm running my application on a rooted Nexus 7 and even though I can get an application to set the time from the GPS signal (Smart GPS Time, works like a champ), my code crashes immediately when I try it. Any idea what's missing?? – Rich Jan 24 '14 at 15:42
0

Normal user applications does not have permission to change the device time. Read the answer by cashbash in the following post for the alternate option.

Community
  • 1
  • 1
Cilenco
  • 6,951
  • 17
  • 72
  • 152
0

Unfortunately, blackbelt is right; android lets us do a lot of cool things, but changing system time programmatically is not one of them.

Since I see that you are looking for more credible/official sources, I suggest you check out this open ticket with Google, which suggests this is an open problem--it ought to work, but doesn't, and it doesn't seem Google is going to fix it anytime soon. The gist of it is that the SET_TIME protection level is set higher than it ought to be. (for more information on permissions, see here)

Although this is not quite the same as changing the time programmatically, you can still make the user change the system time for you if for some reason you do need system time to be changed. This thread will explain how to go about implementing that if you want to go that route.

Hope this was helpful information!

Community
  • 1
  • 1
Ryan Chipman
  • 393
  • 2
  • 5
  • 15