Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use voice command API in Android [closed]

Tags:

android

I am new in android and currently working on small application that works on Voice Command API. For Example if I say bluetooth it will switch the phone's bluetooth to ON/OFF mode (vice-a-versa).

Please help me to do this....

Thanks in Anvance...

like image 288
dinesh sharma Avatar asked Jul 29 '11 12:07

dinesh sharma


1 Answers

It's rather straight forward to use:

private void startVoiceRecognitionActivity() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //uses free form text input
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //Puts a customized message to the prompt
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
        getString(R.string.listenprompt));
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

/**
 * Handles 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<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);

        //Turn on or off bluetooth here
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

And then call the startVoiceRecognitionActivity() from within your code wherever you need it. Of course you'll need to have the permssion to access the internet

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

in your Android.manifest.

like image 174
keyboardsurfer Avatar answered Oct 23 '22 12:10

keyboardsurfer