在语音识别中获取结果的第一个数据

时间:2011-08-25 07:33:44

标签: android voice-recognition

在我的项目中,我将使用语音识别。在语音识别代码中,arraylist返回活动结果。但我只是想把列表中的第一个数据。我的意思是在项目中用户将使用说话按钮,说话,然后看到结果。然后会有继续按钮,显示结果的第一个数据。如何将result参数传递给另一个活动类。但不是最初的列表,只是一个字符串。

1 个答案:

答案 0 :(得分:0)

我使用此代码

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


            if(matches.lenght()>0)  {
                 result = matches.get(0)

                 //Time to create the new intent to the other activity  
                 Intent i  = new Intent (Activity1.this, Activity2.class);
                 i.putExtra("RESULT", result); //RESULT is the key 
                 startActivityForResult(i,ID); // or startActivity(i)
            }
        }

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

然后在另一个活动中你必须要使用bundle并提取字符串

    Bundle bundle = getIntent().getExtras();
    if( (bundle!=null) && (bundle.contains("RESULT"){
        String text = bundle.getString("RESULT");

    }
    else{
        //other things
        return;
    }