Question

I am creating a little application that receives battery data (through the BatteryManager) and when battery level gets 100% it emits a sound to the user every 5 seconds to let him know battery is fully charged.

The problem is that when application gets paused the thread emitting the sound stops too. Is there any way to keep this thread alive? I have been reading about services too but don't know if this is suitable for such a simple thing. Using a service I suppose I could track the battery level there and emit the sound in the service.

Could anyone comment a "good way" to achieve this?

Thanks in advance.

Was it helpful?

Solution

Use a Service definitely. Don't forget to declare it in the manifest just like your activities. Regarding the sound you play, SoundPool is designed to play short audio files, like game effects or notification sounds. To play music files use MediaPlayer.

OTHER TIPS

Service is a good solution. Another way to solve this would be to set Repeating Alarms http://developer.android.com/training/scheduling/alarms.html for every 5 seconds to trigger your code.

Service is the best thing for this. and play sound as a background in it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top