如何正常关闭运行语音识别器内容的Android服务?

时间:2014-07-29 02:33:40

标签: android service google-glass

我已经实现了SilentVoiceRecognitionService的一个版本。

但是,我遇到服务未正常关闭的问题,以至于下次启动应用程序时,服务会抛出ERROR_RECOGNIZER_BUSY。

如果有帮助,这就是我的服务在Manifest中的解析方式:

    <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>

有没有人知道为什么这个SilentVoiceRecognitionService没有正确关闭?

2 个答案:

答案 0 :(得分:0)

也许尝试在您的服务/活动的onDestroy中添加stopService

答案 1 :(得分:0)

我仍然不确定为什么在启动应用时已经有一个识别器已经在监听。但是,在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);
    }