In my app, am using SpeechRecognizer directly. I destroy SpeechRecognizer onPause of the Activity and I recreate it in onResume method as below …
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;
}
}
When the app is installed via eclipse for the first time, SpeechRecognition service is called and recognition happens properly.But when app comes back from pause, if i try to recognize speech i get “SpeechRecognition: not connect to recognition service” error
What am i doing wrong ?
I found the reason to the problem. In
onPausemethod thoughSpeechRecognition.destroy()method is called, I guess it just detaches the service but the objectsrwill be pointing to some instance and it wont be null. Resetting the objectsrto null would solve the problem.Not destroying the
SpeechRecognitionobject inonPausemethod would block other apps from usingSpeechRecognitionservice