¿Cómo cerrar con gracia el servicio de Android que ejecuta un RecognizerIntent de voz?
-
20-12-2019 - |
Pregunta
He implementado una versión del Servicio de reconocimiento de voz silencioso.
Sin embargo, tengo un problema con el servicio que no se cierra correctamente hasta el punto de que la próxima vez que se inicia la aplicación, el servicio arroja un ERROR_RECOGNIZER_BUSY.
En caso de que ayude, así es como se declara mi servicio en el Manifiesto:
<service
android:name="com.xyz.SilentVoiceRecognitionService"
android:label="@string/app_name">
</service>
SilentVoiceRecognitionService se inicia desde el servicio principal de la aplicación (en onStartCommand):
public int onStartCommand(Intent intent, int flags, int startId) {
if (mLiveCard == null) {
[...]
startService(new Intent(this, SilentVoiceRecognitionService.class));
}
return START_STICKY;
}
Ese servicio se inicia una vez.Aquí está la declaración en el archivo de manifiesto:
<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>
¿Alguien tiene una idea de por qué este SilentVoiceRecognitionService no se cierra correctamente?
Solución 2
Todavía no estoy seguro de por qué hay un reconocedor que ya escucha al lanzar la aplicación.Sin embargo, las pruebas de su estado en OnError (), cancelando el reconocedor y reiniciar la escucha se trabajaron para mí.
Aquí está el código que utilicé:
@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);
}
Otros consejos
Tal vez intente poner un stopService
en su onDestroy de su servicio/actividad.