I want to develop a sleep tracking app that should work even when device's screen gets off. I'm curious about whether Service
or Handler
go to sleep when device screen is off. I learned the life-cycle of activity, so I don't think a Handler
would work since activities go to sleep when device's screen is off, and Handlers
are dependent on its activity. But the Service
is a background process, so I think it might work.
So I want the device to be alive even when device's screen is off. Could you give me some advice for this?
Your application will be paused after a while if device screen is locked. The CPU will sleep until user wakes the device, so, your Service
won't do anything while device sleeps.
If you want to perform some action even when the screen is turned off, you can acquire a WakeLock
: http://developer.android.com/reference/android/os/PowerManager.WakeLock.html
But be careful, using the WakeLock
too much will drain battery fast.
Also, another class that may be useful for you is WakefulBroadcastReceiver
: https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html
What it does is, it receives some Intent
and acquires a WakeLock
for you so that you can finish your work in a Service
. Without this you have no guarantee that device will finish the work and not go to sleep.