DroidBender
DroidBender

Reputation: 7892

Android Voice Recognition Commands

Goal

Voice recognition starts, a voice command is spoken and the correct action is done. (Play Some Music starts the music player of whatever supposed to happen.)

Current situation

I have a test application running which start the Android Voice Recognition, successfully listens and returns a result to my Activity.

Snippet to start voice recognition:

 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak your mind.");
 startActivityForResult(intent, REQUEST_CODE);

Snippet for the result:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            // matches hold the spoken words
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

What would be the best approach for this problem?

Upvotes: 1

Views: 965

Answers (1)

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

Easiest way is..

 if (matches.contains("close"))
        {
            finish();
        }

Upvotes: 1

Related Questions