mobileDeveloper
mobileDeveloper

Reputation: 894

How do I use voice search and VoiceRecognition on Android?

I want to use VoiceRecognition in my application, but this application needs to install voice search.

I don't want the user to have to install another other application then return to my application to run it. I want voice search to be installed from my application, or alternatively I'd like to find a tutorial to on how to add Voice Search capability to my application.

What can I do?

Upvotes: 9

Views: 6017

Answers (5)

K_Anas
K_Anas

Reputation: 31466

Use the RecognizerIntent to fire the speech recognizer installed on your device

Upvotes: 5

shivampip
shivampip

Reputation: 2144

Here is a Simple way to Handle Voice Search

Step 1 Call this method on button click

public void startVoiceRecognition() {
    Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
    intent.putExtra("android.speech.extra.LANGUAGE_MODEL", "free_form");
    intent.putExtra("android.speech.extra.PROMPT", "Speak Now");
    this.mContainerActivity.startActivityForResult(intent, 3012);
}

Step 2 Override onActivityResult method

@ Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 3012 && resultCode == RESULT_OK) {
        ArrayList < String > matches = data.getStringArrayListExtra("android.speech.extra.RESULTS");
        String result= matches.get(0);
        //Consume result 
        edittext.setText(result);
    }
}

Thats all, DONE

Upvotes: 0

shammer64
shammer64

Reputation: 313

This can be done in a few simple steps:

  1. Create some sort of button in your activity, and place the following code in its OnClickListener:

    // Define MY_REQUEST_CODE as an int constant in your activity...I use ints in the 10000s startVoiceRecognitionActivity(MY_REQUEST_CODE, "Say something.");

  2. Override the onActivityResult() method in your activity. In the implementation, place a switch block or if statement to run some logic when the requestCode argument matches your MY_REQUEST_CODE constant. Logic similar to the following will get you the list of results the speech recognition activity thought it heard:

    ArrayList keywordMatches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

  3. You may get 0 or many matches from the recognizer. Be sure to handle all cases.

  4. In some cases, the speech recognizer may not even be on the device. Try to handle that where you call startVoiceRecognitionActivity().

Upvotes: 0

Related Questions