Android的SpeechRecognizer应该只从应用程序的主线程中使用主线、应用程序、Android、SpeechRecognizer

2023-09-04 06:08:35 作者:最后、結束

我想整合一些机器人语音的API在我的基础AndEngine游戏。

I am trying to integrate some of Androids Speech APIS in my AndEngine based game.

我把我的code中的BaseGame活动中 - 但这个错误出现在运行时:

I placed my code within the BaseGame activity - however this error appears at runtime:

05-06 23:51:28.955:ERROR / AndroidRuntime(553):java.lang.RuntimeException的:SpeechRecognizer只应该用于从应用程序的主线程

05-06 23:51:28.955: ERROR/AndroidRuntime(553): java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread

我如何访问这些应用程序主线程?我怎么能保证它在我的演讲code运行。

How do I access the applications main thread? And how can I ensure my Speech code runs in it.

的设置如下:

主类 - BaseGameActivity SpeechClass

Main Class - BaseGameActivity SpeechClass

主类实例化一个新的SpeechClass +调用它的方法 - 然后我得到上述错误

Main Class instantiates a new SpeechClass + calls its method - then I get the above error.

请指教我如何能正确地做到这一点。

Please advise on how I can do this correctly.

推荐答案

通过做这样的事情打电话给 SpeechClass 在主线程:

Call the SpeechClass on the main thread by doing something like this:

final SpeechClass c = getSpeechClass(); // get the speech class

View v = findViewById(R.id.anyview); //fetch a View: any one will do

v.post(new Runnable(){ public void run(){ c.doSomething(); }});

这应该在主线程上运行。可能会降低你的UI下来,如果 DoSomething的需要一段时间,但你得到工作了这一点。

That should run it on the main thread. Might slow your UI down if doSomething takes a while, but you get to work that out.

 
精彩推荐
图片推荐