When I press the sleep button on the device then turn it back and "update" the uptime shows the time as if the device did not go to sleep.
-
1Well was it in "deep" sleep? – Jon Skeet Apr 04 '14 at 21:15
-
I think that display dark means that it in "deep" sleep – boroloro Apr 04 '14 at 21:25
-
I don't think it does, necessarily. If you hit the power switch and then hit it again 10 seconds later, I wouldn't expect that to count as "deep" sleep. I'm not an expert on Android power states by any stretch of the imagination though. – Jon Skeet Apr 04 '14 at 21:39
-
"I think that display dark means that it in "deep" sleep" -- the state of the CPU is only loosely connected to the state of the screen. The screen can be off, and the CPU can be on, if something is holding a `WakeLock` to keep it on. – CommonsWare Apr 04 '14 at 22:21
2 Answers
From Android Reference: SystemClock:
uptimeMillis() is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls), Object.wait(millis), and System.nanoTime().
This clock is guaranteed to be monotonic, and is suitable for interval timing when the interval does not span device sleep. Most methods that accept a timestamp value currently expect the uptimeMillis() clock.
Standard functions like Thread.sleep(millis) and Object.wait(millis) are always available. These functions use the uptimeMillis() clock; if the device enters sleep, the remainder of the time will be postponed until the device wakes up. These synchronous functions may be interrupted with Thread.interrupt(), and you must handle InterruptedException.
--
Seems like your phone is idle or power saving and not technically sleeping...
Assuming the button you are talking about is the "power" or "lock" button, that isn't necessarily putting the device into deep sleep. In fact, looking at the definition of deep sleep (when the uptime counter will stop) I think it would be very rare. The phone will almost certainly keep the CPU running at a low level to monitor for alarm timers and other system scheduled events. If you are wanting to monitor how much time the phone is in "sleep" (screen off, phone locked etc) I suspect you will need to register a broadcast receiver and monitor for those specific events.
take a look at this thread Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF? also take a look at the power manager