3

I am trying to do something in java app (android) and I need something to delay/wait for an amount of seconds for a loop. How can I do delay android function? I have tried to use Thread.sleep(), TimeUnit.sleep, but it is only going to do irresponsible program for some seconds. I want to do some onClick actionlistener which updating for some seconds. e.g: if I clicked to button -> Text is changed to random(int) and it's should be done per second.

random ... waiting for a second ... random ... waiting for a second ... random ... and so many times

for (int i = 0; i < 10; i++) {
    int random = r.nextInt(100) - 10;
    String rand = Integer.toString(random);
    textView3.setText(rand);
    TimeUnit.SECONDS.sleep(1);
}
phen0menon
  • 2,354
  • 2
  • 17
  • 36

7 Answers7

2

Use Handler with postDelayed, example:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    Log.d("Log:", "Hello!");
    handler.postDelayed(this, 1000);
  }
}, 1000);
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
0

You can use Handler:

for (int i = 0; i < 10; i++) {
    Handler handler = new Handler();
    Runnable r = new Runnable() {
        public void run() {
        int random = r.nextInt(100) - 10;
        String rand = Integer.toString(random);
        textView3.setText(rand);            
        }
    };
    handler.postDelayed(r, 1000);
}

look at this question: How to run a Runnable thread in Android?

Community
  • 1
  • 1
Palejandro
  • 2,092
  • 5
  • 26
  • 38
0

I'm not very familiar with android app programming

But if you want a random number is printed to the text for every one second... how about using Timer instead of Delay?

I don't know how the code in Android works but the Logic should be like this:

Button Pressed:

Timer.Start(1000)

For every timer tick:

int numberVariable = random(1,10)
textVariable = numberVariable.toString()
Akeno Yuki
  • 13
  • 3
0

Add a handler with a timer, like this:

public Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Do something after 5s = 5000ms
    }
}, 5000);
gi097
  • 7,313
  • 3
  • 27
  • 49
0

You can use the Handler class to delay the loop for whatever amount of time you want. It goes like this.

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Your function goes here.
        }
    }, 5000); /your time in micro seconds.

Hope it helps.

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
0

There is actually a lot of ways to do this.

From: https://stackoverflow.com/a/3072338/2801237

The benefit of this is that you don't need to deal with a handler, and you can easily randomize the '5' seconds input (plus it is clear that it is 5 seconds), not 5000ms. (more below)

private static final ScheduledExecutorService worker = 
          Executors.newSingleThreadScheduledExecutor();

void someMethod() {
  ⋮
  Runnable task = new Runnable() {
    public void run() {
      /* Do something… */
    }
  };
  worker.schedule(task, 5, TimeUnit.SECONDS);
  ⋮
}

and another big benefit of this approach is that you can expand this quickly/easily to use threadpool.

  private static final ScheduledExecutorService worker =
         Executors.newScheduledThreadPool(4); //thread pool of 4 threads. 
Community
  • 1
  • 1
mawalker
  • 2,072
  • 2
  • 22
  • 34
0

Try this

public void randomStart() {
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 1000*1);
            try {
                Random r = new Random();
                int random = r.nextInt(100) - 10;
                String rand = Integer.toString(random);
                textView3.setText(rand);
            }
            catch(Exception e) {
            e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable,1*1000);
}
Milos Lulic
  • 627
  • 5
  • 22