Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何从我的应用程序中使用命令语音打开语音输入_Android_Voice Recognition_Google Now - Fatal编程技术网

Android:如何从我的应用程序中使用命令语音打开语音输入

Android:如何从我的应用程序中使用命令语音打开语音输入,android,voice-recognition,google-now,Android,Voice Recognition,Google Now,使用此代码: private void promptSpeechInput() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra

使用此代码:

private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}
通过不同的方式(按钮事件、手势、生命周期),我可以调用此方法打开语音输入提示,就像我在google搜索字段中单击麦克风图标一样。 好的,很好

但现在,我需要使用命令语音调用此方法。与google now中的方式相同,我们说“Ok google”,然后语音输入提示出现。我想在我的应用程序中执行此操作。例如,我在主活动中,我说“听我说”,然后调用promptSpeechInput方法

我该怎么做

感谢您的帮助。

通过,您的应用程序可以注册,其中之一是“搜索”(这样您就可以在应用程序上搜索一些问题或命令)


在过去,一些开发人员能够。获得批准后,用户可以通过语音对您的应用程序执行特定操作。这不再是一个选项。

据我所知,你不能声明你自己的唤醒词——你只能使用OK Google。您可以使用LychmanIT引用的Voice Actions API,但这只会在说“OK Google”后处理意图


也许值得研究一下。它允许您定义自定义侦听器,但不是此工作流的Google认可的解决方案,因此它可能不适合您,具体取决于您的需求。不过,我已经在原型设计方面取得了一些成功——它工作得非常好

谢谢@SimonPhoenix,这似乎对我有帮助。我将使用它创建一个测试项目,我和我来警告你。谢谢@LychmanIT,我已经测试过了,效果很好。这不是我认为的解决方案的最佳方式,但我可以做到这一点:我可以在所有屏幕上启用“Ok Google”。当用户在应用程序中时,他现在可以使用google进行搜索,我可以得到这个命令并调用一个方法。这似乎不是一个漂亮的解决方案,但用户只需使用语音命令即可操作应用程序。我创建了一个测试项目,但我遇到了一个问题。用英语搜索效果很好,但我来自巴西,搜索甚至不适用于YouTube。所以,我会尝试另一种方式。是的,看起来@Simon_Phoenix的选择比我的好。祝你好运:)