I'm developing an application that is meant to run only as a service. That is, it has no Activity
(that is usually run, at least), and at any given moment, the only component of the application that will be running (usually) is a service. This is meant to be used with the screen locked. It already works.
In such a scenario, I seem to understand that the service's thread is the application's "main thread", even though it's not a UI thread proper (as there is no UI).
The question is: if the service uses a HandlerThread
, can I call runOnUIThread()
from a method that is called from the HandlerThread
? Will this make it run from the main thread, without the need to launch an Activity
(which would involve unlocking the screen)?
Basically my problem is that I need to use a SpeechRecognizer
, lanching it from the service.
Right now my service is using a Handler
on a HandlerThread
. When trying to init a SpeechRecognizer
from methods that are (indirectly) called by the HandlerThread
, I get an exception because SR has to be run from the main thread.
Can I use runOnUIThread()
?
I see there is a similar question here: How to invoke Speechrecognizer methods from service with no main thread or activity
However, the answer involves invoking the SR from onCreate()
or onStartCommand()
, which is not viable in my case.
UPDATE: obviously, I can not call runOnUIThread()
, as it is a method of Activity
. So is there a way to have some call run on the main thread, which is in this case not a UI thread?