如何打开 activity 从语音到文本输出

how to open an activity from a speech to text output

代码如下:

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' 声明是否有效!

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

        }

希望有用。