如何在Android上实现识别器

时间:2012-11-30 09:35:46

标签: android voice-recognition

我的软件架构如下:
TabActivity是TabHost活动 它包含2个ActivityGroup:AGroup和BGroup AGroup包含2个活动:A1Activity和A2Activity。

我想在A1Activity中实现识别器 我的代码如下:

    private static final int VOICE_RECOGNIZER_REQUEST_CODE = 0x1008;
public void Recognizera() {
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

        if(activities.size() != 0) {
            try {   
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "語音辨識");
                startActivityForResult(intent, VOICE_RECOGNIZER_REQUEST_CODE);
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
}
@Override
protected void onActivityResult(int RequestCode, int ResultCode, Intent data) {
    switch(RequestCode) {
    case VOICE_RECOGNIZER_REQUEST_CODE:
        if(RequestCode == VOICE_RECOGNIZER_REQUEST_CODE && ResultCode == RESULT_OK) {
            ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            for(int i = 0; i < results.size(); i++) {
                System.out.println("results " + results.get(i));
            }
        }
        break;
    }
    super.onActivityResult(RequestCode, ResultCode, data);
}

但它显示“未知问题”,如下图所示 enter image description here
但是在logcat中没有任何错误消息 如何修改?

1 个答案:

答案 0 :(得分:1)

这是因为在语音识别期间遇到错误。 ResultCode参数将是错误代码,其中一个错误为here。首先找出该错误是什么,以便进一步调查。