我正在Android Studio上进行语音识别。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private TextView voiceInput;
private TextView speakButton;
private final int REQ_CODE_SPEECH_INPUT = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceInput = (TextView) findViewById(R.id.voiceInput);
speakButton = (TextView) findViewById(R.id.btnSpeak);
speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askSpeechInput();
}
});
}
// Showing google speech input dialog
private void askSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Hi speak something");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
}
}
// Receiving speech input
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voiceInput.setText(result.get(0));
}
break;
}
}
}
}
您可以看到最后一部分是输出的 负责任 :
// Receiving speech input
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voiceInput.setText(result.get(0));
}
break;
}
}
}
我的问题
现在,我创建了第二个活动,并希望在那里接收输出,因此我在上面复制了这些代码块并将其粘贴。它没有用。
如何让语音输入的输出出现在第二个活动中?
如果您需要更多信息,请让我知道,对于这个愚蠢的问题,我感到抱歉,我认为解决方案非常简单,但我无法对其进行罚款!
答案 0 :(得分:0)
尝试将结果数据发送到第二个活动中,如下面的代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voiceInput.setText(result.get(0));
Intent intent=new Intent(this,SecondActivity.class);
intent.putStringArrayListExtra("result",result);
startActivity(intent);
}
break;
}
}
}
onCreate方法中的第二个活动,在下面的行中调用以获取结果。
result_list = getIntent().getStringArrayListExtra("result");
之后,result_list数据将显示到textview和其他视图中。
确保两个活动都定义到android清单文件中。
答案 1 :(得分:0)
You can pass an ArrayList<E> to Your SecondActivity.
示例:
ArrayList<String> yourList = new ArrayList<String>();
intent.putExtra("yourlist", yourList);
在另一个活动中:
ArrayList<String> yourList = (ArrayList<String>)
getIntent().getSerializableExtra("yourlist");