如何打开从语音到文本输出的活动

时间:2016-01-28 10:13:37

标签: android speech-recognition

以下是代码:

public void promptSpeechInput(){
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!");

    try{
        startActivityForResult(i, 100);
    }
    catch(ActivityNotFoundException a){
        Toast.makeText(VoiceSearch.this, "Sorry! Your device doesn't support Speech Language!", Toast.LENGTH_LONG).show();
    }

}

public void onActivityResult(int request_code, int result_code, Intent i){
    super.onActivityResult(request_code,result_code,i);

    switch(request_code){
        case 100: if(result_code == RESULT_OK && i != null){
            ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            resultTEXT.setText(result.get(1));
            String text = String.valueOf(result);
            Toast.makeText(getApplicationContext(),text, Toast.LENGTH_LONG).show();
            if (text.equals("speedometer") ){
                getWindow().setExitTransition(new Explode());
                Intent intent = new Intent(getApplicationContext(), SpeedometerTest.class);
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

            }
        }
            break;
    }
}

我不知道'if'语句是否有效!

1 个答案:

答案 0 :(得分:1)

String s = result.get(0);
if (s.equals("speedometer") ){
            Intent intent = new Intent(getApplicationContext(), SpeedometerTest.class);
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

        }
希望它能起作用。