android意图将语音匹配到另一个活动

时间:2016-12-06 04:09:42

标签: android

如何通过使用语音到文本来进行另一项活动,识别特定单词...对于例如,当我点击按钮并说出“打开”这个词时,它可以去或意图到另一个活动.. 以下代码来自this  教程。

public class main extends Activity implements OnClickListener {
    public ListView mList;
    public Button speakButton, next;
    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        speakButton = (Button) findViewById(R.id.btn_speak);
        next = (Button) findViewById(R.id.next);
        speakButton.setOnClickListener(this);
        voiceinputbuttons();
    }

    public void voiceinputbuttons() {
        speakButton = (Button) findViewById(R.id.btn_speak);
        mList = (ListView) findViewById(R.id.list);
    }

    public void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }

    public void onClick(View v) {
        startVoiceRecognitionActivity();
    }

    public void informationMenu() {
        startActivity(new Intent("android.intent.action.next")); //next is the activity class that i want to go
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it
            // could have heard
            ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, matches));
            // matches is the result of voice input. It is a list of what the
            // user possibly said.
            // Using an if statement for the keyword you want to use allows the
            // use of any activity if keywords match
            // it is possible to set up multiple keywords to use the same
            // activity so more than one word will allow the user
            // to use the activity (makes it so the user doesn't have to
            // memorize words from a list)
            // to use an activity from the voice input information simply use
            // the following format;
            // if (matches.contains("open")) { startActivity(new
            // Intent("android.intent.action.next"));

            if (matches.contains("open")) {
                informationMenu();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

             public void informationMenu() {

                  Intent i = new Intent(this, next.class);
                  startActivity(i); //next is the activity class that i want to go
             }
你自己找到了吗