向其他活动提供数据实际上不起作用



代码是关于什么的:
使用我的代码,我可以打开麦克风,对着它说话并查看输出。


现状 现在,如果我对着麦克风说话,我会看到输出并直接跳转到其他活动中。

要求
我想在活动中获取输出,但在我发短信之前,我向您展示了代码。

主要活动.java

private TextView voiceInput;
private ImageView 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 = (ImageView) 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, "Sprechen Sie was ein");
//tent.putStringArrayListExtra("result",resultat);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
}
}
@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) {
Intent intent=new Intent(this,Zweites.class);
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
voiceInput.setText(result.get(0));
intent.putStringArrayListExtra("resultat",result);
//ActivityZweites wird gestartet
startActivity(intent);
}
break;
}
} }} <br>

在那我遇到了以下问题:

if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//Ausgabe in andere Activity
voiceInput.setText(result.get(0));
Intent intent=new Intent(this,Zweites.class);
intent.putStringArrayListExtra("resultat",result);
//ActivityZweites wird gestartet
startActivity(intent);
}
break;
}

在那里,它输出语音输入:

voiceInput.setText(result.get(0));

之后,它跳入活动。
如何在不先显示输出并跳入另一个活动的情况下在活动中获取此输出?

我尝试了一些事情,但没有找到解决此问题的解决方案。
你们有什么想法吗?

亲切问候

如果您只想将相同的文本传递给您在 TextView 中显示的意图,请交换

intent.putStringArrayListExtra("resultat",result);

intent.putExtra("resultat", result.get(0));

然后,在Zweites活动中,您可以像这样获取字符串:

Intent intent = getIntent();
String text = intent.getStringExtra("resultat");

最新更新