- Forum posts: 18
Jul 30, 2018, 7:39:40 PM via Website
Jul 30, 2018 7:39:40 PM via Website
Hi.
I have code bellow. Please, would you tell me how to wait if I call getSpeechInput(); (bottom of the code) while TextView is filled and then call again getSpeechInput(). I tried Thread.sleep, but it doesn't work.
Thank you.
package com.example.oco.SpeechToText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Pools;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
import java.util.concurrent.SynchronousQueue;
import static java.lang.Thread.sleep;
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 Don't 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 test(View view) throws InterruptedException {
getSpeechInput();
**Thread.sleep(10000);//Wait when first getSpeechInput is finished**
getSpeechInput();
}
}
— modified on Jul 30, 2018, 9:16:15 PM
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.