Reputation: 2462
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);
}
Upvotes: 3
Views: 1872
Reputation: 627
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);
}
Upvotes: 0
Reputation: 2070
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.
Upvotes: 0
Reputation: 1750
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.
Upvotes: 0
Reputation: 7701
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);
Upvotes: 0
Reputation: 7083
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);
Upvotes: 2
Reputation: 13
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()
Upvotes: 0
Reputation: 2339
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?
Upvotes: 0