android - 如何在 Android 中使用语音命令 API

标签 android

<分区>

我是 android 新手,目前正在开发适用于 Voice Command API 的小型应用程序。例如,如果我说蓝牙,它会将手机的蓝牙切换到开/关模式(反之亦然)。

请帮我做这个....

感谢 Anvance...

最佳答案

使用起来相当简单:

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);
    }
}

然后在您需要的任何地方从您的代码中调用 startVoiceRecognitionActivity()。当然你需要有访问互联网的权限

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

在你的 Android.manifest 中。

关于android - 如何在 Android 中使用语音命令 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6872853/

相关文章:

android - 在列表已经更新和绘制后,如何为单个项目(条目)设置动画?

java - RecyclerView 在滚动时崩溃

android - 自动截断 TextView 文本以免与另一个 TextView 重叠

android - 抽屉导航不适用于 AppCompat 主题

android - 如何让其他应用像 Google 云端硬盘应用一样访问我的应用数据

用于 alpha 测试模式的 Android 应用内购买

android - 如何在android中录制音频m4a格式的音频文件?

android - 修改 Google App Engine 端点 (Android) 中的 HTTP header

android - FragmentPagerAdapter 未正确删除项目( fragment )

android: google map api 总是从非洲开始显示