我使用活动来调用谷歌语音识别活动。我的问题是,当 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;
}
}
}
}