如何根据语音命令打开Activity

时间:2013-01-21 04:59:57

标签: android voice voice-recognition

我正在做有很多屏幕的仪表板应用程序。当用户根据我需要打开活动告诉语音命令时。我不知道从哪里开始我已经完成了所有的屏幕,我想实现语音搜索。 我的应用程序屏幕是Advances,Leaves,Recruitment,Permissions,Notifications等 示例:当用户说“进展”时,它应该打开进度屏幕。请帮帮我。

1 个答案:

答案 0 :(得分:3)

1)启动语音识别意图

2)处理onActivityResult()中返回的数据以决定启动哪个活动

<强> 1。启动语音识别意图

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Choose Activity");
startActivityForResult(intent, REQUEST_SPEECH);

<强> 2。处理返回的数据

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == REQUEST_SPEECH){
            if (resultCode == RESULT_OK){
                ArrayList<String> matches = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                if (matches.size() == 0) {
                    // didn't hear anything
                } else {
                    String mostLikelyThingHeard = matches.get(0);
                    // toUpperCase() used to make string comparison equal
                    if(mostLikelyThingHeard.toUpperCase().equals("ADVANCES")){
                        startActivity(new Intent(this, Advances.class));
                    } else if() {
                    }
                }
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }