package com.androidcookbook.speechrecognizerdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.List; public class Main extends Activity { private static final int RECOGNIZER_RESULT = 1234; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startSpeech = (Button)findViewById(R.id.getSpeechButton); startSpeech.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech to text"); startActivityForResult(intent, RECOGNIZER_RESULT); } }); } /** * Handle the results from the recognition activity. */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RECOGNIZER_RESULT && resultCode == RESULT_OK) { List<String> matches = data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); TextView speechText = (TextView)findViewById(R.id.speechText); speechText.setText(matches.get(0).toString()); } super.onActivityResult(requestCode, resultCode, data); } }