Developing Android Applications with Voice Recognition Features
Highlights
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.

A voice recognition sample
- 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
02 | * A helper class for speech recognition |
04 | public class SpeechRecognitionHelper { |
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. |
11 | * @param callingActivity = Activity, that initializing recognition process |
13 | public static void run(Activity callingActivity) { |
14 | // check if there is recognition Activity |
15 | if (isSpeechRecognitionActivityPresented(callingActivity) == true) { |
16 | // if yes – running recognition |
17 | startRecognition(callingActivity); |
19 | // 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); |
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
02 | * Checks availability of speech recognizing Activity |
04 | * @param callerActivity – Activity that called the checking |
05 | * @return true – if Activity there available, false – if Activity is absent |
07 | private static boolean isSpeechRecognitionActivityPresented(Activity callerActivity) { |
09 | // getting an instance of package manager |
10 | PackageManager pm = callerActivity.getPackageManager(); |
11 | // a list of activities, which can process speech recognition Intent |
12 | List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); |
14 | if (activities.size() != 0) { // if list not empty |
15 | return true; // then we can recognize the speech |
17 | } catch (Exception e) { |
21 | return false; // we have no activities to recognize the speech |
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.
02 | * Send an Intent with request on speech |
03 | * @param callerActivity - Activity, that initiated a request |
05 | private static void startRecognitionActivity(Activity callerActivity) { |
07 | // creating an Intent with “RecognizerIntent.ACTION_RECOGNIZE_SPEECH” action |
08 | Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
10 | // giving additional parameters: |
11 | intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Select an application"); // user hint |
12 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); // setting recognition model, optimized for short phrases – search queries |
13 | intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); // quantity of results we want to receive |
14 | //choosing only 1st - the most relevant |
16 | // start Activity ant waiting the result |
17 | ownerActivity.startActivityForResult(intent, SystemData.VOICE_RECOGNITION_REQUEST_CODE); |
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.
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 |
06 | private static void installGoogleVoiceSearch(final Activity ownerActivity) { |
08 | // creating a dialog asking user if he want |
09 | // to install the Voice Search |
10 | Dialog dialog = new AlertDialog.Builder(ownerActivity) |
11 | .setMessage("For recognition it’s necessary to install "Google Voice Search"") // dialog message |
12 | .setTitle("Install Voice Search from Google Play?") // dialog header |
13 | .setPositiveButton("Install", new DialogInterface.OnClickListener() { // confirm button |
15 | // Install Button click handler |
17 | public void onClick(DialogInterface dialog, int which) { |
19 | // creating an Intent for opening applications page in Google Play |
20 | // Voice Search package name: com.google.android.voicesearch |
22 | // setting flags to avoid going in application history (Activity call stack) |
23 | intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
25 | ownerActivity.startActivity(intent); |
26 | } catch (Exception ex) { |
27 | // if something going wrong |
32 | .setNegativeButton("Cancel", null) // cancel button |
35 | dialog.show(); // showing dialog |
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.
01 | // Activity Results handler |
03 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
05 | // if it’s speech recognition results |
06 | // and process finished ok |
07 | if (requestCode == SystemData.VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { |
09 | // receiving a result in string array |
10 | // there can be some strings because sometimes speech recognizing inaccurate |
11 | // more relevant results in the beginning of the list |
12 | ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); |
14 | // in “matches” array we holding a results... let’s show the most relevant |
15 | if (matches.size() > 0) Toast.makeText(this, matches.get(0), Toast.LENGTH_LONG).show(); |
18 | super.onActivityResult(requestCode, resultCode, data); |
The created class SpeechRecognitionHelper allows us to perform a speech recognition request by calling only one function run().
Team Digit is made up of some of the most experienced and geekiest technology editors in India!
View full profile