我有下面的代码。请告诉我,如果我在TextView填充时调用getSpeechInput();(代码的底部),然后再次调用getSpeechInput(),该如何等待。我试过Thread.sleep,但它不起作用。谢谢。
package com.example.oco.test1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
import static java.lang.Thread.*;
public class MainActivity extends AppCompatActivity {
private TextView txvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txvResult = (TextView) findViewById(R.id.txvResult);
}
public void getSpeechInput() {
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());
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 10);
} else {
Toast.makeText(this, "Your Device Dont Support Speech Input", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 10:
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txvResult.setText(result.get(0));
}
break;
}
}
public void getSTT(View view) {
getSpeechInput();
Thread.sleep(5000);
getSpeechInput();
}
}
发布于 2018-08-01 05:22:57
您正在启动一个活动,因此当它完成时,onActivityResult将被触发。在调用getSpeechInput之后,放入你想要做的任何事情。
https://stackoverflow.com/questions/51622159
复制相似问题