3

我正在做仪表板应用程序,其中有很多屏幕。当用户根据我需要打开活动来告诉语音命令时。我不知道从哪里开始我已经完成了所有的屏幕,我想实现语音搜索。我的应用程序屏幕是预付款、请假、招聘、权限、通知等示例:当用户说“预付款”时,它应该打开预付款屏幕。请帮我。

4

1 回答 1

3

1)启动语音识别意图

2)在onActivityResult()中处理返回的数据来决定启动哪个activity

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);
    }
于 2013-01-21T06:01:12.840 回答