Это простая реализация SpeechRecgnizer, но Toast, присутствующий в методе onResults, отображается дважды. Я знаю, что это можно решить с помощью некоторых логических флагов, но почему метод onResults выполняется дважды?
speech.startListening (speechIntent) вызывается только один раз, а onResults выполняется два раза.
Ничего подобного не упоминается в документации здесь https://developer.android.com/reference/android/speech/RecognitionListener
public class MainActivity extends AppCompatActivity implements RecognitionListener{
Button micButton;
SpeechRecognizer speech;
int flag=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
micButton=findViewById(R.id.mic_button);
final Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,MainActivity.this.getPackageName());
micButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speech.startListening(speechIntent);
Toast.makeText(MainActivity.this, "Listening", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onReadyForSpeech(Bundle params) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float rmsdB) {
}
@Override
public void onBufferReceived(byte[] buffer) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int error) {
}
@Override
public void onResults(Bundle results) {
ArrayList<String> output = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Toast.makeText(this, output.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
}