4

Android doesn't appear to provide a way for a user application to change the system time. What I would like to do instead is to get the user to change the time. It is easy to open up the Date & Time settings:

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

What I would like to know is:

  1. Is it possible to link directly to the set time option?
  2. Is it possible to check that the user set the time correctly? I am aware of the TIME_CHANGED broadcast message, but I can't find any documentation on it

Update:

The TIME_CHANGED broadcast message doesn't provide any information about how it changed. This isn't explicitly documented, but I tried getData and getExtras and received no information. Additionally, looking at the rest of the documentation it appears that no data or extras are passed unless explicitly documented. Similarly, it is actually documented the ACTION_DATE_SETTINGS takes no input and it is implicit that it takes no extras, so there is no way to control it more precisely.

CommonsWare pointed out what should have been obvious to me - that simply checking that the user set the time to the value retrieved from the server, without worrying about the time spent in the options menu, will almost always work. As this assumption could actually be false, if I were using this method, then I would ensure the message said that the time was probably set correctly or incorrectly, rather than using a definite statement.

However, I discovered elapsedRealtime and so I will actually implement this detection properly.

Community
  • 1
  • 1
Casebash
  • 114,675
  • 90
  • 247
  • 350

3 Answers3

7

Is it possible to link directly to the set time option?

Try:

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

Is it possible to check that the user set the time correctly?

:: shrug ::

After the application gets control again, examine the time.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I already provided that intent in the question. I was asking whether you could link directly to the set time option, rather than just the general time/date settings. Only one click, but still, I like to make it as convenient as possible for the user. – Casebash May 10 '10 at 12:04
  • But how would I account for the time that the user spends in the time settings app? I can't use the system clock, as the user is changing it. It generally won't be significant, but suppose a friend interrupts them and they finish setting the time 15 minutes later. I know this is a bit of a silly case, but an application should never say that something is correct when it isn't. – Casebash May 10 '10 at 12:11
  • Sorry -- wasn't paying attention. If the time is more critical to you than it is to the user, then you should not be relying upon the user at all. Use an NTP client and get the time yourself. I seem to recall some posts on the `android-developers` list about somebody who got an NTP client going. – CommonsWare May 10 '10 at 12:24
  • When the time is retrieved, I give the user the option of using the time offset for this session or changing the time manually. Anyway, I just discovered that I can use [elapsedRealtime](http://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime%28%29) to handle the adjustment, rather than trying to figure out the broadcast message. Actually, that method would allow me to figure out the time change for a broadcast message if I really needed to. – Casebash May 10 '10 at 12:38
1

In case someone wants to open from ADB

adb shell am start -a android.settings.DATE_SETTINGS
SubqueryCrunch
  • 1,325
  • 11
  • 17
0

I think the intent of the question just goes against andoid design philosophy: If you could send the user directly to the clock to change the time this would automatically set the time to not be set automatically by the device (or some ntp server) but by the user. The user might not be aware of that and wonder why the clock never auto corrects itself wich it used to do.

If you want to be sure of the time you need to do the work and implement a kind of your own in-app timeserver: Get the time using a sntp internet time server request and then start your own precise timer to count the seconds - I don't think it would be realistic to get much better than 1 Second. DO NOT USE Thread.sleep() it is not precise by my tests. I use ScheduledExecutorService.scheduleAtFixedRate I did only some testing on two devices and it stayed within 1 Second for about 3 hours when I said good enough. Also there is the problem that everything will stop sometime after android goes sleep mode.

If you already get GPS Positions you can also use the time of those to fix. Just never use the first it could be a last known Position from Memory. Remember the time of last fix and use only for synch when time of new fix is higher/newer.

FrankKrumnow
  • 501
  • 5
  • 13