0

Is there a way I can delay an if statement for 1 sec before running the other if condition? I tried using a run-able handler for each statement but it crashes the app, I also used Thread.sleep(1000); but it just freezes the UI. How do I solve this?

This is how I first used the handler

final Handler Internet_handler = new Handler();
       final Runnable internet = new Runnable() {
           public void run() {if (EseCards_Slot1.isShown()) {
   EseCards_Slot1.performClick();
   bot_count++;
   Stop_Two_InstantPLay(); // stops 2 cards from playing i.e pick 2 and ride on
   botThinkdelay();// delays the if statement
}
           }
       };
       Internet_handler.postDelayed(internet, 1700);
if (EseCards_Slot1.isShown()) {
   EseCards_Slot1.performClick();
   bot_count++;
   Stop_Two_InstantPLay(); // stops 2 cards from playing i.e pick 2 and ride on
   botThinkdelay();// delays the if statement
}
if (EseCards_Slot2.isShown()) {
   EseCards_Slot2.performClick();
   bot_count++;
   Stop_Two_InstantPLay();
   botThinkdelay();
}
if (EseCards_Slot3.isShown()) {
   EseCards_Slot3.performClick();
   bot_count++;
   Stop_Two_InstantPLay();
   botThinkdelay();
}
if (EseCards_Slot4.isShown()) {
   EseCards_Slot4.performClick();
   bot_count++;
   Stop_Two_InstantPLay();
   botThinkdelay();
}
if (EseCards_Slot5.isShown()) {
   EseCards_Slot5.performClick();
   bot_count++;
   Stop_Two_InstantPLay();
   botThinkdelay();
}
if ((bot_count>0)&&(Ese_Turn_toPlay==true)) {
   Market.performClick();
   bot_count=0;
   botThinkdelay();
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

I think, you can use Handler (see How to set delay in android? or How to use delay functions in Android Studio?). The code becomes ugly, but I know only a few solutions in Java for UI thread (CountDownTimer).

import android.os.Handler;

final Handler handler = new Handler();

if (EseCards_Slot1.isShown()) {
   EseCards_Slot1.performClick();
   bot_count++;
   Stop_Two_InstantPLay(); // stops 2 cards from playing i.e pick 2 and ride on
   handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (EseCards_Slot2.isShown()) {
               EseCards_Slot2.performClick();
               bot_count++;
               Stop_Two_InstantPLay();
               // Here comes the next Handler.
               // It would be better to divide if statements into methods
               // and run them inside each new handler.postDelayed.
            }
        }
    }, 1000);

}

You should remove all handlers after you close the screen.

CoolMind
  • 26,736
  • 15
  • 188
  • 224