本文介绍了如何调用从服务Speechrecognizer方法,没有主线程或活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,有数十家#1和有关SpeechRecognizer类以及如何实现它的其他网站的链接。我一直在通过几个人,并搜查了几个小时。
虽然在服务实现的例子,他们似乎并不为我工作。

I am aware that there are dozens of links on Stackoverflow and other sites about the SpeechRecognizer class and how to implement it. I have been through several of them and have searched for hours.Although there are examples of implementations in services, they don't seem to work for me.

我的应用程序有一个活动,但它仅用于用户设置和时,我的服务运行时,它从系统中单独调用不运行。因此,我的服务没有任何主应用程序线程涉及到以运行/调用SpeechRecognizer类的方法。

My application has an activity, however it is only used for user settings and does not run when my service runs, which is called separately from the system. Thus my service does not have any main application thread to relate to in order to run/invoke the methods of the SpeechRecognizer class.

由于它在Android文档中阐明,:

As it is stated in the android docs, here:

这个类的方法必须只能从主应用程序线程调用。

当我运行我的code,包括这是我的服务类SpeechRecognizer.startListening(mSpeechRecognizerIntent)方法我收到以下错误:

When I run my code including the SpeechRecognizer.startListening(mSpeechRecognizerIntent) method which is in my service class I get the following error:

了java.lang.RuntimeException:SpeechRecognizer应该只从应用程序的主线程中使用

有没有办法解决这个问题的方法吗?我已阅读了SpeechRecognizer可以识别语音的背景或从服务运行时',我发现这里:

Is there a way to solve this problem? I have read that the SpeechRecognizer can 'recognize speech while running in the background or from a service', I found that here:

Comparison语音识别在Android中使用:由意向或线程

所以这基本上是我想我只是似乎无法得到SpeechRecognizer开始,因为我得到的提到的错误。
你也可以告诉我是否有必要调用应用程序的主线程下面的方法(,这我没有):

So that is basically what I want I just can't seem to get the SpeechRecognizer started because I get the mentioned error.Could you also tell me whether it is necessary to call the following methods in the application's main thread (,which i don't have):

mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(本);
  mSpeechRecognizer.setRecognitionListener(新SpeechRecognitionListener());

我的code基本上等同于这个链接的上方答案code:(不IncomingHandler类,我相信只是用来控制监听的时间)

My code is basically identical to the code in the top answer of this link: (without the IncomingHandler class, which I believe is only used to control the timing of the listener)

Android语音识别是在Android 4.1及放大器的服务; 4.2

真的AP preciate一些帮助,在此先感谢。
这是我的第一个Android应用程序,所以我pretty新的吧,请多多包涵;)

Would really appreciate some help, thanks in advance.This is my first android application so I am pretty new to it, please bear with me ;)

推荐答案

您不需要活动进入主线程,服务将工作得很好。

You do not need an activity to access the main thread, a service will work just fine.

现在,我没有你的code,但我假设你正在使用的或相似。
这对因为问题 是在工作线程激活IntentService.onHandleIntent()

Now, I do not have your code but I assume you are using an IntentService or similar.This poses a problem since IntentService.onHandleIntent() is activated on a worker thread

要解决,无论是:

一个。放置Speechrecognizer调用逻辑上IntentService.onCreate()
它总是被称为主线程,或

A. place the Speechrecognizer invocation logic on IntentService.onCreate()which is always called on the main thread, or

乙。开关使用标准的服务和放在onStartCommand你的逻辑()
被称为主线程的方法是,再次保证。

B. switch to using standard service and place your logic in onStartCommand()method which is, again, guaranteed to be called on the main thread.

这篇关于如何调用从服务Speechrecognizer方法,没有主线程或活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 23:05