I want to develop a foreground service to produce sounds. However, everything that I try only works when I’m debugging, and once I disconnect the cable and the phone is on its own, I don’t get what I want.
What I want is a service that uses CountDownTimer to execute a task every X interval, even when the phone sleeps.
To do that I used the following 2 options, and each had its disadvantages:
1) Make a foreground service using startForeground(); Ended up with a background service. The service just goes to sleep when the phone does, and every once in a while the OS runs it (I can hear the sounds).
Once I open the phone to its lock screen, the service becomes alive and immediately produce the sounds.
2) Make use of WakeLock; This works even when the phone sleeps, but now the phone’s “go to sleep” time-out is disabled, but I still want it on so that the user doesn’t have to press the lock button.
Can someone please advise me on what can I do ?
The
AlarmManagermay be what you are looking for.http://developer.android.com/reference/android/app/AlarmManager.html