Recipe Launching an Activity for a Result Using Speech to Text

In this recipe, launching an activity for a result is demonstrated. It also demonstrates how to utilize speech to text functionality from Google's Recognizerlntent and print the result to the screen. Here, the trigger event is a button press. It launches the Recognizerlntent activity, which does speech recognition on sound from the microphone and converts it into text.When finished, the text is passed back to the calling activity.

Upon return, the onActivityResult() function is first called with the returned data, and then the onResume() function is called to continue the activity as normal.The calling activity can have a problem and not return properly. Therefore, the resultCode should always be checked to ensure result_OK before continuing to parse the returned data.

Note that in general any launched activity that returns data causes the same onActivityResult() function to be called.Therefore, a request code is customarily used to distinguish which activity is returning.When the launched activity finishes, it returns control to the calling activity and calls onActivityResult() with the same request code.

The steps for launching an activity for result are

1. Call startActivityForResultQ with an intent, defining the launched activity and an identifying requestCode.

2. Override the onActivityResultQ function to check on the status of the result, check for the expected requestCode, and parse the returned data.

The steps for using Recognizerlntent are

1. Declare an intent with action ACTION_RECOGNIZE_SPEECH.

2. Add any extras to the intent; at least EXTRA_LANGUAGE_MODEL is required. This can be set as either LANGUAGE_MODEL_FREE_FORM or LANGUAGE_MODEL_WEB_SEARCH.

3. The returned data bundle contains a list of strings with possible matches to the original text. Use data.getStringArrayListExtra to retrieve this data.This should be cast as an ArrayList for use later.

A TextView is used to display the returned text to the screen. The main activity is shown in Listing 2.14.

The additional supporting files needed are the main.xml and strings.xml, which need to define a button and the TextView to hold the result. This is accomplished using Listing 2.10 and 2.12 in the "Launching Another Activity from an Event" recipe.The AndroidManifest needs to declare only the main activity, which is the same as the basic "Creating an Activity" recipe. The RecognizerIntent activity is native to the Android system and does not need to be declared explicitly to be utilized.

Listing 2.14 src/com/cookbook/launch_for_result/RecognizerIntent package com.cookbook.launch_for_result;

import java.util.ArrayList;


import android.content.Intent;

import android.os.Bundle;

import android.speech.RecognizerIntent;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class RecognizerIntentExample extends Activity { private static final int RECOGNIZER_EXAMPLE = 1001; private TextView tv;

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

tv = (TextView) findViewById(;

//setup button listener

Button startButton = (Button) findViewById(; startButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) {

// RecognizerIntent prompts for speech and returns text Intent intent =

new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word or phrase\nand it will show as text"); startActivityForResult(intent, RECOGNIZER_EXAMPLE);

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //use a switch statement for more than one request code check if (requestCode==RECOGNIZER_EXAMPLE && resultCode==RESULT_OK) {

// returned data is a list of matches to the speech input ArrayList<String> result =


//display on screen tv.setText(result.toString());

super.onActivityResult(requestCode, resultCode, data);

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment