How To: Voice Commands into an android application

Sam Bevins picture Sam Bevins · Aug 3, 2012 · Viewed 67.7k times · Source

There are many tutorials online for adding voice recognition to an android app. They are often confusing and the publishers of the coding are never available for questions. I need a basic overview of how to add voice recognition to my app (as an answer, not a link).

Answer

Sam Bevins picture Sam Bevins · Aug 3, 2012

If you want to add voice recognition to your group's Android app it is very simple.

Throughout this tutorial you will need to add imports as you paste in the code.

  1. Create an xml file or use an existing one and make sure that you add a button and a listview.
  2. In a java class you need to extend activity and implement OnClickListener You will get an error that says you have unimplemented methods. Hover over it and add the unimplemented methods. We will add to this later.
  3. Next set up the button and listview in your java.

    public ListView mList;
    public Button speakButton;
    

    also add:

    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    
  4. Next, make an OnCreate Method and set up the button and listener.

    speakButton = (Button) findViewById(R.id.btn_speak);
    speakButton.setOnClickListener(this);
    

    also add this method(we will set it up next)

    voiceinputbuttons();
    

    Remember to setContentView for the xml you are showing.

  5. Outside of your oncreate make a new method that looks like this.

    public void voiceinputbuttons() {
        speakButton = (Button) findViewById(R.id.btn_speak);
        mList = (ListView) findViewById(R.id.list);
    }
    
  6. Now you will have to set up your voice recognition activity by using the following code.

    public void startVoiceRecognitionActivity() {
        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 recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }
    
  7. Next, inside your onclick method from step 2 add the activity from step 6.

    startVoiceRecognitionActivity();
    
  8. Next we will have to set up another method. Copy and paste the following code.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));
    

    matches is the result of voice input. It is a list of what the user possibly said. Using an if statement for the keyword you want to use allows the use of any activity if keywords match it is possible to set up multiple keywords to use the same activity so more than one word will allow the user To use the activity (makes it so the user doesn't have to memorize words from a list) To use an activity from the voice input information simply use the following format;

    if (matches.contains("information")) {
        informationMenu();
    }
    

    NOTE: you can format the code any time by pressing ctrl+shift+F in eclipse.

  9. Now we are going to set up our method used by the code in step 8. This code creates an intent to direct the user to a new menu. You will need another xml and java class for this. Also, remember to add an activity to your manifest.

    public void informationMenu() {
        startActivity(new Intent("android.intent.action.INFOSCREEN"));
    }
    
  10. Finally you need to set up some code that will let the user know if the mic is operational. Paste this code inside of the OnCreate method at the end.

    // Check to see if a recognition activity is present
    // if running on AVD virtual device it will give this message. The mic
    // required only works on an actual android device
    PackageManager pm = getPackageManager();
    List activities = pm.queryIntentActivities(new Intent(
    RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() != 0) {
        voiceButton.setOnClickListener(this);
    } else {
        voiceButton.setEnabled(false);
        voiceButton.setText("Recognizer not present");
    }
    

FINAL NOTE: Voice Recognition will not work on a virtual emulator because they can't access the mic on your computer. The voice recognition will only work with an internet connection.

This is approx. what your final code should look like in your java.

package com.example.com.tutorialthread;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.speech.RecognizerIntent;
import android.support.v4.app.NavUtils;

public class main extends Activity implements OnClickListener {

public ListView mList;
public Button speakButton;

public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    speakButton = (Button) findViewById(R.id.btn_speak);
    speakButton.setOnClickListener(this);

    voiceinputbuttons();
}

public void informationMenu() {
    startActivity(new Intent("android.intent.action.INFOSCREEN"));
}

public void voiceinputbuttons() {
    speakButton = (Button) findViewById(R.id.btn_speak);
    mList = (ListView) findViewById(R.id.list);
}

public void startVoiceRecognitionActivity() {
    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 recognition demo");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    startVoiceRecognitionActivity();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        // Fill the list view with the strings the recognizer thought it
        // could have heard
        ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, matches));
        // matches is the result of voice input. It is a list of what the
        // user possibly said.
        // Using an if statement for the keyword you want to use allows the
        // use of any activity if keywords match
        // it is possible to set up multiple keywords to use the same
        // activity so more than one word will allow the user
        // to use the activity (makes it so the user doesn't have to
        // memorize words from a list)
        // to use an activity from the voice input information simply use
        // the following format;
        // if (matches.contains("keyword here") { startActivity(new
        // Intent("name.of.manifest.ACTIVITY")

        if (matches.contains("information")) {
            informationMenu();
        }
    }
}