Our target device must have at least one application that can process the Intent for speech recognition, which is called by theRecognizerIntent.ACTION_RECOGNIZE_SPEECH action.
Android can’t recognize speech, so a typical Android device cannot recognize speech either.
Or, is there a way it can? The easiest way is to ask another application to do the recognition for us. Asking another application to do something in Android is called using intents.
Our target device must have at least one application that can process the Intent for speech recognition, which is called by theRecognizerIntent.ACTION_RECOGNIZE_SPEECH action.
One such app is Google Voice Search. It is one of the best recognizers available for Android and supports a lot of languages. This service requires Internet connection because the voice recognition occurs on Google servers. This app has a very simple Activity that informs users they can speak. The moment the user stops talking, the dialog is closed and our application (intent caller) receives an array of strings with the recognized speech.
A voice recognition sample
Let’s write a little sample app that demonstrates using voice search in applications.
Our application needs to do these things:
Receive a request for voice recognition
Check the availability of application for speech recognizing
If speech recognizing is available, then call the intent for it and receive the results
If speech recognizing is not available, then show the dialog for installing Google Voice Search and redirect the user to Google Play, if he wants
First, we create a class that implements the logic for speech recognition. Call this class SpeechRecognitionHelper where we declare a static, public function run() that will receive a request for launching a recognition:
01
/**
02
* A helper class for speech recognition
03
*/
04
publicclassSpeechRecognitionHelper {
05
06
/**
07
* Running the recognition process. Checks availability of recognition Activity,
08
* If Activity is absent, send user to Google Play to install Google Voice Search.
09
* If Activity is available, send Intent for running.
10
*
11
* @param callingActivity = Activity, that initializing recognition process
// if no, then showing notification to install Voice Search
20
Toast.makeText(callingActivity, "In order to activate speech recognition you must install "Google Voice Search"", Toast.LENGTH_LONG).show();
21
// start installing process
22
installGoogleVoiceSearch(callingActivity);
23
}
24
}
25
}
As you can see, besides the run() function we need to implement three other functions:
isSpeechRecognitionActivityPresented – checks if the speech recognition application is present on the system
installGoogleVoiceSearch – initializes the Google Voice Search installation process
startRecognition – prepares the appropriate Intent and runs the recognition
To check if the device has an application for speech recognition, we can use the queryIntentActivities method in class PackageManager. This method gives a list of activities that can process the specified Intent. To receive an instance of the PackageManager, we can use getPackageManager.
Our code is shown below:
isSpeechRecognitionActivityPresented
01
/**
02
* Checks availability of speech recognizing Activity
03
*
04
* @param callerActivity – Activity that called the checking
05
* @return true – if Activity there available, false – if Activity is absent
// a list of activities, which can process speech recognition Intent
12
List activities = pm.queryIntentActivities(newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
13
14
if(activities.size() != 0) { // if list not empty
15
returntrue; // then we can recognize the speech
16
}
17
} catch(Exception e) {
18
19
}
20
21
returnfalse; // we have no activities to recognize the speech
22
}
Now implement the startRecognition function. This function will form the appropriate Intent for launching the speech recognition Activity. You can find detailed information for how to do it on documentation page.
Source code:
01
/**
02
* Send an Intent with request on speech
03
* @param callerActivity - Activity, that initiated a request
And last, we’ll implement the installGoogleVoiceSearch. This function will show the dialog, asking the user if he wants to install Google Voice Search and send him to Google Play, if he does.
01
/**
02
* Asking the permission for installing Google Voice Search.
03
* If permission granted – sent user to Google Play
04
* @param callerActivity – Activity, that initialized installing
That’s about it. We run the speech recognition Activity. Then request the user’s permission to install Voice Search and send him to Google Play if he consents. One thing we still need to do and that is gather the voice recognition results.
We send a request using the startActivityForResult function to gather results of the launched Activity. We also need to redefine a OnActivityResultmethod in our intent caller Activity. This can be done this way:
The created class SpeechRecognitionHelper allows us to perform a speech recognition request by calling only one function run().
All that is needed for adding a recognition feature is to add this class in our project and call the run function in needed place. And then implement processing text results by redefining the onActivityResult method for the Activity that initiated the recognition call.
For additional information you can look at the Android Developers website. Here, you’ll find good examples showing how to do voice recognition, and importantly, how to get the available language list. You will need this list if you want to recognize a language other than the user’s default locale.
For fast integration of voice input in to your app, you can download and use this code for the SpeechRecognitionHelper class.