1

我使用活动来调用谷歌语音识别活动。我的问题是,当 Google Speech Reg Activity 已经弹出时,我们可以在主要活动中收听 keyevent 吗?

有任何想法吗?不过,我知道当 Google Activity 弹出时,主要活动将是 onPause。

这是我的安卓代码

public class TestSpeechReg extends Activity {
private TextView txtText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Button startButton = new Button(this);
    startButton.setText("Start TestSpeechReg");
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             startSpeechReg(TestSpeechReg.this);
        }
    });

    txtText = new TextView(this);
    txtText.setText("###################");

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.addView(startButton);
    layout.addView(txtText);

    setContentView(layout);
}

// voice recognition
public static final int REQUESTCODE_SPEECHRECOGNITION = 0x1000;

private void startSpeechReg(Activity context) {
    Log.i("TestSpeechReg", "openSpeechRecognition");
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    context.startActivityForResult(intent, REQUESTCODE_SPEECHRECOGNITION);
}

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

    switch (requestCode) {
    case REQUESTCODE_SPEECHRECOGNITION: {
        if (resultCode == RESULT_OK && null != data) {
            ArrayList<String> text = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            txtText.setText(text.get(0));
        }
        break;
    }
    }
}
}
4

1 回答 1

0

如果您想控制 UI(例如听按键),则通过SpeechRecognizer类(需要 API 级别 8)与语音识别器进行交互,这不会启动新的活动。您当前的代码启动了一个新活动,即您的 UI 已停止(即,我认为不仅是onPause,而且还被调用)。onStop

于 2012-11-07T20:56:22.023 回答