Voice Recognition API in Android Apps Using RecognizerIntent

Voice interface is not a common application user interface, we do not usually interact with application using voice. But when it comes to mobile it makes more sense.
We are used to talk to a phone all day – why not talk to our phone apps?
Not all voice interaction should look like this video:


Here is how you use Android Voice Recognition API:

  1. Call the RecognizerIntent  – your application call the Voice recognition Intent that records the voice.
  2. Intent processes the voice recording – sends it to the voice Recognition Service and returns
  3. A list of string is passed to an implemented callback method in you application
  4. You process the string and continue the interaction with the user

Here is a simple call to the intent:

     /**
     * Fire an intent to start the speech recognition activity.
     */
    private 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);
    }


And here is the callback:

      /**
     * Handle the results from the recognition activity.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent 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);   

          //DO SOMETHING WITH THE MATCHES

        super.onActivityResult(requestCode, resultCode, data);
    }

Here is a full code sample – I played with it today and will very little effort created a Pizza ordering application for my next demo session.

Share the love...Tweet about this on TwitterShare on LinkedInShare on Google+Share on Facebook

Amir Shevat

Amir Shevat is the global Startup Outreach lead in Google Developer Relations (g.co/launch). Previously, Amir Led Google Campus Tel Aviv and was the co founder of several startups.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *