简体   繁体   English

屏幕关闭时如何让应用程序停止

[英]how to make app stop when screen turns off

I have created an exercise app with text-to-speech functionality that says exercise name and sound when it's time to rest.我创建了一个具有文本转语音功能的锻炼应用程序,它会在 rest 时说出锻炼名称和声音。 but when I turn the screen off app keeps working making the sound and saying the exercise name.但是当我关闭屏幕时,应用程序会继续发出声音并说出锻炼名称。 I think it has something to do with activity lifecycle, but I am a beginner in android development, so I don't quite know them.我认为它与活动生命周期有关,但我是 android 开发的初学者,所以我不太了解它们。

How can I make the app stop working when the screen is off?屏幕关闭时如何使应用程序停止工作?

You have to pause your TTS manager when your activity/fragment goes to the onPause()/onStop() state and becomes not visible to the end user.当您的活动/片段进入 onPause()/onStop() state 并且对最终用户不可见时,您必须暂停 TTS 管理器。

Please, read about activities/fragments lifecycle and determine which state would be the best solution for your case.请阅读活动/片段生命周期并确定哪个 state 将是您案例的最佳解决方案。 You can either override these lifecycle methods directly (the easiest way) or you can use the Jetpack Lifecycle lib to create lifecycle-aware components (a cleaner approach if your TTS manager implementations exists as a separate class (and it should be so)).您可以直接覆盖这些生命周期方法(最简单的方法),也可以使用Jetpack Lifecycle lib来创建生命周期感知组件(如果您的 TTS 管理器实现作为单独的 class 存在(应该是这样),这是一种更简洁的方法)。

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

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