Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用语音识别器启动Android活动?_Android_Switch Statement_Speech - Fatal编程技术网

如何使用语音识别器启动Android活动?

如何使用语音识别器启动Android活动?,android,switch-statement,speech,Android,Switch Statement,Speech,我想改变这个开关,这样我的活动将通过说出相关水果的名称来启动,而不是单击按钮。例如,Apple类将通过说出“Apple”一词来启动。我应该如何重写此开关?到目前为止,我所有的尝试似乎都没有奏效。如能提供任何答案,将不胜感激。谢谢大家! package com.example.speech; import com.example.speech.R; import android.app.Activity; import android.content.Intent; import android

我想改变这个开关,这样我的活动将通过说出相关水果的名称来启动,而不是单击按钮。例如,Apple类将通过说出“Apple”一词来启动。我应该如何重写此开关?到目前为止,我所有的尝试似乎都没有奏效。如能提供任何答案,将不胜感激。谢谢大家!

package com.example.speech;

import com.example.speech.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // find our buttons and set onClickListeners for all
        ImageButton one = (ImageButton)findViewById(R.id.one);
        ImageButton two = (ImageButton)findViewById(R.id.two);
        ImageButton three = (ImageButton)findViewById(R.id.three);
        ImageButton four = (ImageButton)findViewById(R.id.four);
        ImageButton five = (ImageButton)findViewById(R.id.five);
        ImageButton six = (ImageButton)findViewById(R.id.six);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
    }

    public void onClick(View v) {

        // do something based on the button that was clicked
        switch(v.getId()) {
            case R.id.one:
                // create an intent indicating we want
                // to start the activity.

                            Intent i = new Intent(this, Apple.class);
                // start the activity based on the Intent
                startActivity(i);
                finish();
                break;

            case R.id.two:
                    Intent j = new Intent(this, Orange.class);

                    // start the activity based on the Intent
                    startActivity(j);
                    finish();
                    break;

            case R.id.three:
                    Intent k = new Intent(this, Banana.class);

                    // start the activity based on the Intent
                    startActivity(k);
                    finish();
                    break;

            case R.id.four:
                    Intent l = new Intent(this, Grape.class);

                    // start the activity based on the Intent
                    startActivity(l);
                    finish();
                    break;

            case R.id.five:
                    Intent m = new Intent(this, Strawberry.class);

                    // start the activity based on the Intent
                    startActivityForResult(m, 0);
                    finish();
                    break;

            case R.id.six:
                    Intent n = new Intent(this, Kiwi.class);

                    // start the activity based on the Intent
                    startActivity(n);
                    finish();
                    break;
                    default:
                    finish();
        }

    };
}
这是处理语音活动结果的正确方法吗

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

            //If Voice recognition is successful then it returns RESULT_OK
            if(resultCode == RESULT_OK) {

                ArrayList<String> textMatchList = data
                .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                if (!textMatchList.isEmpty()) {
                    // If first Match contains the word 'apple'
                    // Then start the apple activity.
                    if (textMatchList.get(0).contains("apple")) {
                            // create an intent indicating we want
                // to start the activity.

                            Intent i = new Intent(this, Apple.class);
                // start the activity based on the Intent
                startActivity(i);
                finish();
                break;

这是一个关于如何使用Android语音识别的示例教程。您需要在OnActivityResult中处理搜索结果,并对苹果、香蕉、葡萄等进行字符串匹配。。如果找到字符串匹配项,则启动相应的活动

你做得对。您可以使用RecognizerIntent启动活动(如示例代码中的-check startVoiceRecognitionActivity())。完成后,它将使用resultcode调用onActivityResult,在那里检查字符串匹配,然后开始相应水果的活动。
if (textMatchList.get(0).contains("apple")) {