简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM