4

I want to make an image be visibile for 60 ms and then be invisible, then I want another image to do the same.. and so on. I don't think I'm using the Timer right.. because when I run the app both images turn on at the same time and don't disappear when I press the button that uses this function.

Here's some 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 another alternative? I've tried examples like.. Android app How to delay your Service start on phone boot

and

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

But it's not doing what I want..

Anything I'm doing wrong? Or is there an alternative way that 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);
Community
  • 1
  • 1
faul
  • 227
  • 2
  • 4
  • 13
  • possible duplicate - http://stackoverflow.com/questions/2005656/android-timer-swing – mre Jun 08 '11 at 19:29

3 Answers3

2

You can try to use Handler, it posts smth into UI thread, it can post with a delay postDelayed

Dmitri Gudkov
  • 2,093
  • 16
  • 15
  • 1
    When yo do use a handler remember to cancel callback onPause :-) – Ravi Vyas Jun 08 '11 at 19:33
  • Hey, so I'm looking into Handler's but I am unsure in how to implement it. I've been reading the Android Developers Reference pages and some sample code but I'm unsure in how to translate what I already have into using Handlers. – faul Jun 08 '11 at 20:16
  • [Example](http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android) – Dmitri Gudkov Jun 08 '11 at 20:21
  • Thanks for the example, I tried it but now it only shows the last image to be VISIBLE, not the first one at all when I press the button. Check out the code snippet at my original post. – faul Jun 08 '11 at 22:35
  • And now the first LED starts to flicker o_O – faul Jun 08 '11 at 23:18
0

The problem is that both timers have a 60 ms delay and in the run method of both you set them to be visible. You need to change one of the run methods to set it to invisible.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
  • Alright, giving it a try right now. But one question.. since I MUST put a delay number in ms after the run.. doesn't that mean the ImageView will be "invisible" for 60ms? – faul Jun 08 '11 at 19:30
  • Okay so I tried what you said and now the first LED does not be come "visible" at all.. – faul Jun 08 '11 at 19:35
0

You are creating two events which both fire 60 ms from now.

You instead could set the first event to fire in 60ms and the second in 120ms, or have the first event trigger a submission of the second event 60ms from when the first runs.

matt b
  • 138,234
  • 66
  • 282
  • 345