tags:

views:

509

answers:

1

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.

+3  A: 

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
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
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
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
@Commons: 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