Search code examples
androidservicebatterylevel

Executing a method eventhough activity is paused?


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.


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.