1

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.

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.

As it is stated in the android docs, here:

'This class's methods must be invoked only from the main application thread.'

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 should be used only from the application's main thread

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 of Speech Recognition use in Android: by Intent or on-thread?

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(this); mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());

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 Speech Recognition as a service on Android 4.1 & 4.2

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 ;)

Community
  • 1
  • 1
Iceman
  • 13
  • 4

1 Answers1

1

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

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

To solve, either:

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

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

Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30
  • Thanks for the quick response ;) So as my custom service is called by the android system on an event, I guess option A would be the more appropriate as there is no explicit activity that calls startService(Intent). The only problem is that I plan on invoking the speechrecognizer in another method within/of the service (not onCreate) as I need that method to do something for me first. Is it possible to just retrieve the main thread using a method and then run the invocation code on it? I will think about option A ... if you have any more advice or ideas please tell me, thanks again ;) – Iceman Aug 03 '14 at 22:56