Como desligar normalmente o serviço Android executando um RecognizerIntent de fala?
-
20-12-2019 - |
Pergunta
Eu implementei uma versão do SilentVoiceRecognitionService.
No entanto, tenho um problema com o serviço que não está sendo encerrado corretamente, na medida em que na próxima vez que o aplicativo for iniciado, o serviço gerará um ERROR_RECOGNIZER_BUSY.
Caso ajude, é assim que meu serviço é declarado no Manifesto:
<service
android:name="com.xyz.SilentVoiceRecognitionService"
android:label="@string/app_name">
</service>
O SilentVoiceRecognitionService é iniciado a partir do serviço principal da aplicação (em onStartCommand):
public int onStartCommand(Intent intent, int flags, int startId) {
if (mLiveCard == null) {
[...]
startService(new Intent(this, SilentVoiceRecognitionService.class));
}
return START_STICKY;
}
Esse serviço é iniciado uma vez.Aqui está a declaração no arquivo de manifesto:
<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>
Alguém tem uma ideia de por que este SilentVoiceRecognitionService não foi desligado corretamente?
Solução 2
Ainda não sei por que já existe um reconhecedor escutando ao iniciar o aplicativo.No entanto, testar seu estado em onError(), cancelar o reconhecedor e reiniciar a escuta funcionou para mim.
Aqui está o código que usei:
@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);
}
Outras dicas
Talvez tente colocar um stopService
no seu onDestroy do seu serviço/atividade.