How to gracefully shutdown Android Service running a speech RecognizerIntent?
-
20-12-2019 - |
Question
I have implemented a version of the SilentVoiceRecognitionService.
However, I have an issue with the Service not being shutdown properly to the extent that the next time the application is started, the service throws a ERROR_RECOGNIZER_BUSY.
In case it helps, this is how my service is declated in the Manifest:
<service
android:name="com.xyz.SilentVoiceRecognitionService"
android:label="@string/app_name">
</service>
The SilentVoiceRecognitionService is started from the application main service (in onStartCommand):
public int onStartCommand(Intent intent, int flags, int startId) {
if (mLiveCard == null) {
[...]
startService(new Intent(this, SilentVoiceRecognitionService.class));
}
return START_STICKY;
}
That service is started once. Here's it declaration in the Manifest file:
<service
android:name="com.xyz.XYZService"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data
android:name="com.google.android.glass.VoiceTrigger"
android:resource="@xml/voice_trigger_start" />
</service>
Does anyone have an idea why this SilentVoiceRecognitionService is not shutdown properly?
Solution 2
I am still not sure why there is a recognizer already listening when launching the app. However, testing for its state in onError(), cancelling the recognizer and restarting listening did work for me.
Here's the code I used:
@Override
public void onError(int arg0) {
String mError = "";
switch (arg0) {
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
mError = " network timeout";
break;
case SpeechRecognizer.ERROR_NETWORK:
mError = " network" ;
return;
case SpeechRecognizer.ERROR_AUDIO:
mError = " audio";
break;
case SpeechRecognizer.ERROR_SERVER:
mError = " server";
break;
case SpeechRecognizer.ERROR_CLIENT:
mError = " client";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
mError = " speech time out" ;
break;
case SpeechRecognizer.ERROR_NO_MATCH:
mError = " no match" ;
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
mError = " recogniser busy" ;
mSpeechRecognizer.cancel();
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
mError = " insufficient permissions" ;
break;
default:
mError = "Unknown Error";
}
Log.i(TAG, "Error: " + arg0 + " - " + mError);
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
}
OTHER TIPS
Maybe try putting a stopService
in your onDestroy of your service/activity.