My thread does not wait for code execution to finish and restarts

Asked

Viewed 21 times

0

Thus, the following thread needs to be always running so that if the READ TEXT button is activated (it is a swicth), the Voice class that reads the text on the screen is executed, as well as if the switch is disabled, the function would not be executed and the thread would "sleep" for 1 second and then try to run again... However, she does not wait for the task of speaking the text to finish, causing the text to be repeated several times... Can anyone help me? Thank you.

thread = new Thread() {

           @Override
           public void run() {
               try {
                   while (!thread.isInterrupted()) {
                       runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               if (sw.isChecked()) {
                                   if (frase != null) {
                                       Voice.speak(frase);
                                   }
                               } else {
                                  Voice.off();
                                   Log.i("Tag", "Ta passando");
                               }
                       }
                       });
                      thread.sleep(1000);
                   }
               } catch (InterruptedException e) {
               }
           }
       };

       thread.start();
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.