문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top