如何优雅地关闭Android服务运行演讲evericeRingEntent?
-
20-12-2019 - |
题
我已经实现了 silentvoicerecognitionsservice
但是,我有一个问题与服务没有正常关闭,在下次应用程序启动时,该服务将抛出error_recognizer_busy。 如果它有助于,这就是我的服务在清单中拒绝的情况: <service
android:name="com.xyz.SilentVoiceRecognitionService"
android:label="@string/app_name">
</service>
.
silentvoicerecognitionservice从应用程序主服务启动(在onstartCommand中):
public int onStartCommand(Intent intent, int flags, int startId) {
if (mLiveCard == null) {
[...]
startService(new Intent(this, SilentVoiceRecognitionService.class));
}
return START_STICKY;
}
.
<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>
.
解决方案 2
我仍然不确定为什么在启动应用程序时还有一个识别器已经侦听。但是,在onError()中测试其状态,取消识别器并重新启动聆听为我工作。
这是我使用的代码:
@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);
}
. 其他提示
也许尝试在您的服务/活动的ondestroy中放置一个生成的icetagcode。
不隶属于 StackOverflow