How to add a delay between two actions

I wanted te to delay interstitial with 1 second after pressing the button. I used Thread.sleep () but it didn't work because the message should be shown after the button is pressed also delayef. I want to click a button and wait 1 second for a message and then show an ad.

+3


source to share


2 answers


Perhaps this is what you are looking for:

new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        showMessage();
        ...
      }
    }, ms);

      



This will delay operations run()

for the specified ms

milliseconds.

+3


source


You can use Handler with postDelay. the duration of the pass in milliseconds, then run () is called after the specified duration.



             Handler h = new Handler();
                Runnable r = new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        // code that will run after 1 second(1000 ms)
                    }
                };
                h.postDelayed(r, 1000);

      

+1


source







All Articles