How to wait when method is finished?

  • Replies:1
Luboss8
  • Forum posts: 18

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

Reply
Luboss8
  • Forum posts: 18

Aug 5, 2018, 9:25:02 AM via Website

Thanks.
I've changed my code. See bellow. App waits when I say next words but textViews don't change text. TextViews show only last text and Toast.makeText message show all from memory at the very end. I want to show textView text and toast message after every change "i" in cycle. Can you help me please?

public class MainActivity extends AppCompatActivity {
private TextView txvResult, txvPomoc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txvResult = (TextView) findViewById(R.id.txvResult);
    txvPomoc = (TextView) findViewById(R.id.textView1);
}
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));
                Toast.makeText(this, txvResult.getText(), Toast.LENGTH_SHORT).show();
            }
            break;
    }
}
public void getSTT(View view) {
    for(int i=0;i<8;i++) {
        getSpeechInput();
        txvPomoc.setText("question number: "+i);
        try {
            sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

}

Helpful?
Reply