brian
brian

Reputation: 6912

How to implement recognizer on Android

My software architecture as below:
TabActivity is a TabHost Activity.
It contain 2 ActivityGroup: AGroup and BGroup.
AGroup contain 2 Activities: A1Activity and A2Activity.

I want to implement recognizer in A1Activity.
My code as below:

    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);
}

But it show "Unknown problem" as the picture below URL.
enter image description here
But without any error message in logcat.
How to modify it?

Upvotes: 0

Views: 427

Answers (1)

Jong
Jong

Reputation: 9115

This happens because an error was encountered during the voice recognition. The ResultCode parameter will be the error code, one of the errors here. First find what is that error, so you can investigate it further.

Upvotes: 1

Related Questions