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
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
// 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.
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.