如何在 PeriodicWorkRequest 中使用 SpeechRecognizer? [英] How do I use SpeechRecognizer in a PeriodicWorkRequest?

查看:74
本文介绍了如何在 PeriodicWorkRequest 中使用 SpeechRecognizer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试每 15 分钟进行一次语音识别我正在使用 PeriodicWorkRequest 但我收到错误消息 java.util.concurrent.ExecutionException: java.lang.RuntimeException:SpeechRecognizer 只能在应用程序的主线程中使用.我该怎么做?PeriodicWorkRequest 是错误的方法吗?

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?

推荐答案

如果您尝试使用 Android Framework API 进行语音识别,则该功能仅适用于前台活动.根据该错误,您似乎试图从无法启动的后台收听使用 Android 9.

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.

在最近的 Android 版本中,后台服务变得更加严格,以延长电池寿命.以前,您可能可以使用 前台服务 来维护您的流程,但是通过 Doze Mode 等功能,Google 限制了用例更进一步.即使您遵循豁免建议和您的应用程序已指示用户手动将您的应用程序从设置"中的省电模式"中排除,设备制造商添加了自己的省电功能,例如以下网站:https://dontkillmyapp.com/ 需要了解 Android 中的当前状态.

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.

很可能,您看到的任何后台语音激活或聆听应用都是使用旧版 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.

这篇关于如何在 PeriodicWorkRequest 中使用 SpeechRecognizer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆