开发者

How to use voice command API in Android [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 3 years ago.

开发者_如何学C Improve this question

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜