简体   繁体   English

屏幕超时打乱了文字转语音应用程序

[英]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. 我已经知道FLAG_KEEP_SCREEN_ON解决方案,当应用程序的窗口处于前台时,该解决方案可使屏幕保持打开状态。

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? 这是可能的吗,还是从根本上与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. 您可能需要的是PARTIAL_WAKE_LOCK。 It is described here: 此处描述:

Android PowerManager Android PowerManager

Mark Murphy's Advanced Android book goes into detail on how to create a Service and maintain the proper wakelocks. 马克·墨菲(Mark Murphy)的《高级Android》一书详细介绍了如何创建服务和维护适当的唤醒锁。 He includes source code (on github) showing how to do this. 他包括显示如何执行此操作的源代码(在github上)。 His book is here: 他的书在这里:

The Busy Coder's Guide to Advanced Android Development 忙于高级Android开发的编码人员指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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