Mridang Agarwalla
Mridang Agarwalla

Reputation: 45128

What is the correct method for getting the uptime of the device?

I need to get the uptime of the device and for this I've been using the method called SystemClock.uptimeMillis(). Some users have complained the the uptime statistics aren't correct and the device has been up for a lot longer.

I read the docs but I'm confused as to what method in the SystemClock class will yield the real uptime — should I use elapsedRealtime()?

Upvotes: 1

Views: 149

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007624

That depends on how you define "uptime" with respect to your app.

uptimeMillis() does not count time spent in sleep mode. So, if the user turns on their device, runs it for five minutes before it goes to sleep, then does not return to the device for a week, uptimeMillis() will return a value around five minutes.

elapsedRealtime() does count time spent in sleep mode. So, if the user turns on their device, runs it for five minutes before it goes to sleep, then does not return to the device for a week, elapsedRealtime() will return a value around one week.

Upvotes: 3

Related Questions