Screen timeout disrupts text-to-speech app
I have a simple text-to-speech app that gets disrupted when screen timeout kicks in.
I already know about the FLAG_KEEP_SCREEN_ON solution, which keeps the screen on while the app's window is in the foreground.
My question is whether it is possible to keep the text-to-speech running even after screen timeout kicks in.
Is this开发者_如何转开发 possible at all or does this fundamentally go against the design of Android?
For example, if I re-implement my app as a service, can it continue running while the screen is off?
Yes, I would recommend that you run the text-to-speech converter in a background Service. But even that is not enough. What you need to do is maintain a wakelock. This will keep the device from going to sleep and turning off your converter. There are different kinds of wakelocks. What you probably need is a PARTIAL_WAKE_LOCK. It is described here:
Android PowerManager
Mark Murphy's Advanced Android book goes into detail on how to create a Service and maintain the proper wakelocks. He includes source code (on github) showing how to do this. His book is here:
The Busy Coder's Guide to Advanced Android Development
精彩评论