I am designing an android app that needs to play a sound every minute while in its active state.
I have the logic that plays the sound every minute in the active state using a runnable and handler. The app works fine if you press the start or stop buttons.
If you run the app and hit stop it kills the app. If you run the app hit home the sounds continue to play and when you relaunch the timer will be up to date. This is expected.
The problem i am having is that if you hit home and then launch another app, there is a high chance my app gets killed and the sounds will no longer play (and consequently GUI will is reset when you relaunch)
My question is, What approach should i use so that my app will continue to play the sounds until i tell it to stop, even if its not in the foreground? The sounds can be thought of as a reminder to the user.
I have tried using the google machine to help me out but am having no luck.
Except that it won’t last.
Correct.
A service. With a period of one minute, you’re on the borderline between where I think you should use
AlarmManagerand anIntentServiceand where your existingHandlerapproach can work.You should use
startForeground()with the service, both to keep Android from shutting it down, and to give the user a really easy way to get back to the activity to shut the thing up (via theNotificationyou establish viastartForeground()).