开发者

Making the user change the time in Android

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 wo开发者_高级运维uld 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.


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.


In case someone wants to open from ADB

adb shell am start -a android.settings.DATE_SETTINGS


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜