0

So today I upgraded from gingerbread to Ice Cream sandwich. Other changes I did today was turn wifi on and enable background data.

Now in LogCat I'm seeing this:

Tag: SystemClock Text: Setting time of day to sec=1325630833

Some games I'm making depends on this not changing when the game is playing. I often use System.currentTimeMillis() to determine elapsed time between movements. for example, game objects' velocity is measured in pixels/second. Having Android change the System clock messes this up.

Is there a better way to measure elapsed time?

一二三
  • 21,059
  • 11
  • 65
  • 74

1 Answers1

4

Android Changes the system clock?

Sure. Most computers do, typically syncing against an NTP server somewhere. In the case of phones, depending on the location, they may also respond to NITZ signals from the wireless carrier.

Is there a better way to measure elapsed time?

Use SystemClock, specifically the uptimeMillis() and elapsedRealtime() methods.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think there is no 100% solution. See the doc for uptimeMillis(): Note: This value may get reset occasionally (before it would otherwise wrap around). Additionally, what do you think, what on are these methods depending? Right, on native methods which are finally wrapped into System.nanoTime(). And therefore its just a shift of the problem. – poitroae Jan 04 '12 at 00:01
  • @Michael: "I think there is no 100% solution" -- you are certainly welcome to your opinion. I disagree. "See the doc for uptimeMillis()" -- that will happen once every few weeks and can be easily handled. "Right, on native methods which are finally wrapped into System.nanoTime()" -- and your proof of this is, what, exactly? The source code appears to disagree with you. – CommonsWare Jan 04 '12 at 00:14
  • I think the SystemClock's native methods are saying exactly this. Getting the nanotime for further calculations. The system clocks time does not even change more often. It is just changing for synchronization ;) – poitroae Jan 04 '12 at 00:21
  • @Michael "It is just changing for synchronization" or when the user changes the clock or when it changes to winter/summer time or when the timezone changes. – tidbeck Jan 04 '12 at 00:26
  • elapsedRealTime() was what I was looking for. Thanks! – user1128840 Jan 04 '12 at 00:29