Reputation: 1111
Hello this is not my main code but its close enought to show my problem .
i need to create UI delay (twice) - one after the other i couldnt make it with handler.postDelay alone.
so i have tried making it with Threading .
What i want to do is let t2 start only after t1 ends.
can anyone help me out ?
final Handler handler1 = new Handler();
final Handler handler2 = new Handler();
synchronized(this)
{
Thread t1= new Thread(new Runnable() {
public void run()
{
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.RED);
notifyAll();
}
}, 3000);
}
});
Thread t2= new Thread(new Runnable() {
public void run()
{
handler2.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.YELLOW);
}
}, 3000);
}
});
t1.start();
while (t1.isAlive() )
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
t2.start();
}
Upvotes: 2
Views: 2203
Reputation: 18977
You can also keep it simple in this way:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(3000);
runOnUiThread(new Runnable() {
@Override
public void run() {
imgB1.setBackgroundColor(Color.RED);
}
});
SystemClock.sleep(3000);
runOnUiThread(new Runnable() {
@Override
public void run() {
imgB1.setBackgroundColor(Color.YELLOW);
}
});
}
});
t.start();
Upvotes: 1
Reputation: 28706
Keep it simple :
final Handler handler1 = new Handler(Looper.getMainLooper());
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.RED);
//post another action to be executed in 3 sec
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.YELLOW);
}
}, 3000);
}
}, 3000);
Upvotes: 2