Android 通过“语音”这样的短语启动语音识别;好的,谷歌;?

Android 通过“语音”这样的短语启动语音识别;好的,谷歌;?,android,speech-recognition,Android,Speech Recognition,我正在开发一个应用程序,它使用语音命令来执行某些功能。我有一些密码在工作 private static final int SPEECH\u REQUEST\u code=0; //创建可以启动语音识别器活动的意图 私有void displaySpeechRecognizer(){ 意向意向=新意向(识别意向、行动、识别言语); intent.putExtra(识别器intent.EXTRA_语言_模型, 识别者意图、语言、模型、自由形式); //开始活动时,将用语音文本填充意图 startAc

我正在开发一个应用程序,它使用语音命令来执行某些功能。我有一些密码在工作

private static final int SPEECH\u REQUEST\u code=0;
//创建可以启动语音识别器活动的意图
私有void displaySpeechRecognizer(){
意向意向=新意向(识别意向、行动、识别言语);
intent.putExtra(识别器intent.EXTRA_语言_模型,
识别者意图、语言、模型、自由形式);
//开始活动时,将用语音文本填充意图
startActivityForResult(意图、言语请求代码);
}
//语音识别器返回时调用此回调。
//这是您处理意图并从意图中提取语音文本的地方。
@凌驾
ActivityResult上受保护的void(int请求代码、int结果代码、,
意图(数据){
if(requestCode==SPEECH\u REQUEST\u CODE&&resultCode==RESULT\u OK){
列表结果=data.getStringArrayListExtra(
识别者意图。额外结果);
字符串spokenText=results.get(0);
//用spokenText做点什么
}
super.onActivityResult(请求代码、结果代码、数据);
}
然而,这种方法需要通过点击按钮来激活。有没有办法通过语音命令启动语音识别器?就像现在的谷歌,你可以说“Ok Google”,然后它会打开语音识别器的活动,听命令


谢谢。

您需要编写连续语音识别服务。然后根据你在演讲时得到的输入,检测你的触发短语并采取行动

这可能会占用大量内存,您需要在适当的时间和屏幕上启动和停止服务来进行优化


的公认答案提供了实现类似功能的方法。

借助服务实现连续语音识别:-

GitHub示例:-

回应像Ok谷歌这样的神奇词语

我已经为银行项目实现了相同的功能。我在

private static final int SPEECH_REQUEST_CODE = 0;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        // Do something with spokenText
    }
    super.onActivityResult(requestCode, resultCode, data);
}