Android: Timer / Delay Alternative

I want to make the image visible for 60 ms and then be invisible, then I want the other image to do the same .. and so on. I don’t think that I am using Timer correctly .. because when I launch the application, both images turn on at the same time and do not disappear when I press the button using this function.

Here is a sample code.

timer.schedule(new TimerTask() { @Override public void run() { LED_1.setVisibility(View.VISIBLE); // LED_1 is an ImageView } }, 60); LED_1.setVisibility(View.INVISIBLE); timer2.schedule(new TimerTask() { @Override public void run() { LED_2.setVisibility(View.VISIBLE); // LED_2 is an ImageView } }, 60); LED_2.setVisibility(View.INVISIBLE); 

Is there any other alternative? I tried examples like .. Android application How to delay the start of a service when the phone boots

and

http://www.roseindia.net/java/beginners/DelayExample.shtml

But that does not do what I want.

Anything I'm doing wrong? Or is there an alternative way I can do this?

Thanks.

-Faul

For Good.Dima ..

  int delayRate = 60; final Runnable LED_1_On = new Runnable() { public void run() { LED_1.setVisibility(View.VISIBLE); handler.postDelayed(this, delayRate); } }; handler.postDelayed(LED_1_On, delayRate); final Runnable LED_2_On = new Runnable() { public void run() { LED_1.setVisibility(View.INVISIBLE); LED_2.setVisibility(View.VISIBLE); handler3.postDelayed(this, delayRate); } }; handler.postDelayed(LED_2_On, delayRate); 
+4
source share
3 answers

You can try using Handler, it puts smth in the user interface thread, it can send messages with postDelayed delay

+2
source

The problem is that both timers have a delay of 60 ms, and in the start method of both you set their visibility. You need to change one of the launch methods so that it is invisible.

0
source

You create two events that simultaneously trigger 60 ms.

Instead, you can set the first event to fire at 60 ms, and the second to 120 ms, or the first event triggers the transmission of the second event 60 ms from the moment of the first start.

0
source

All Articles