I am bit confused after going through the questions & answers in Stackoverflow about WakefulIntentService
. I just would like to get some knowledge on this topics to make sure my understanding is correct, please feel free to correct me, if I am wrong.
I built a small application, where I am using a background Service
that keeps playing music whenever the user shakes the mobile. I tested after the device is locked and screen is turned off and it works as expected.
What I am hearing from this forum, the service might turn off as soon the device goes to asleep. Is that true? In my case, it works always, Am I missing something?
What is the need of
WakeFulIntentService
? When do we need to useWakefulIntentService
?I tried running a timer in a
Service
, though the device is locked and screen is turned off and my timer is running pretty much I can say for sure. Because I used to get notification whenever my timer trips.
I used the code below in an app.
Make sure your service is sticky:
I you want your phone to be awake constantly u can use this code below:
Don't forget the permissions in your manifest.
Yes.
Then something else on your device is keeping the device from falling asleep. Perhaps use
adb shell dumpsys power
to see whatWakeLocks
are outstanding.The device may fall asleep if the user is inactive and nothing is keeping the device awake. A
WakeLock
is used to ensure the device stays awake. For transactional-type work (e.g., downloading a file),WakefulIntentService
combines anIntentService
and aWakeLock
to make keeping the device awake as long as necessary (and only as long as necessary) relatively easy.WakefulIntentService
is not suitable for use with services that need to run indefinitely, such as a music player. For those, manage your ownWakeLock
.