How do I use SpeechRecognizer in a PeriodicWorkRequest?

久未见 提交于 2021-01-29 06:04:04

问题


I'm trying to speech recognize every 15 minutes for this I'm using PeriodicWorkRequest but I get the error saying java.util.concurrent.ExecutionException: java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread. How do I go about doing this? Is PeriodicWorkRequest the wrong approach?


回答1:


If you are trying to use Android Framework API to do speech recognition, that functionality is only available with the foreground activity. Based on the error it appears that you are trying to listen from background which isn't possible starting with Android 9.

Background services has gotten stricter in recent versions of Android in order to improve battery life. Previously you might be able to use a Foreground Service to maintain your process, however with features like Doze Mode, Google is restricting the use cases even further. Even if you follow the recommendations for exemption and your app had directed the user manually exempt your app from 'battery saving mode' in Settings, device manufacturers have added their own battery saving features such that a site like: https://dontkillmyapp.com/ is need to understand the current state in Android.

Most likely, any background voice activation or listening app you may have seen was with older versions of Android, or custom software by device manufacturers.



来源:https://stackoverflow.com/questions/63804404/how-do-i-use-speechrecognizer-in-a-periodicworkrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!