在我的应用程序中,我直接使用 SpeechRecognizer。我销毁 Activity 的 SpeechRecognizer onPause 并在 onResume 方法中重新创建它,如下所示...
public class NoUISpeechActivity extends Activity {
protected static final String CLASS_TAG = "NoUISpeechActivity";
private SpeechRecognizer sr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_no_uispeech);
sr = getSpeechRecognizer();
}
@Override
protected void onPause() {
Log.i(CLASS_TAG, "on pause called");
if(sr!=null){
sr.stopListening();
sr.cancel();
sr.destroy();
}
super.onPause();
}
@Override
protected void onResume() {
Log.i(CLASS_TAG, "on resume called");
sr = getSpeechRecognizer();
super.onResume();
}
....
private SpeechRecognizer getSpeechRecognizer() {
if(sr == null){
sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
CustomRecognizerListner listner = new CustomRecognizerListner();
listner.setOnListeningCallback(new OnListeningCallbackImp());
sr.setRecognitionListener(listner);
}
return sr;
}
}
第一次通过 Eclipse 安装应用程序时,会调用 SpeechRecognition 服务并正确进行识别。但是当应用程序从暂停状态返回时,如果我尝试识别语音,则会收到“SpeechRecognition:未连接到识别服务”错误
我究竟做错了什么 ?