RVG
RVG

Reputation: 3576

how to check timer is running on blackberry

I've add timer to display images in my app.

is there any way to check the timer is running or not.?

after checking , the timer should be cancel using timer.cancel() method.

Pls hlp me.

Upvotes: 1

Views: 638

Answers (3)

Ray Vahey
Ray Vahey

Reputation: 3065

You can manage this yourself by recording the timers unique integer and use it later to cancel. I find a useful place to set/cancel this is in the onVisibilityChanged(boolean) override. I'm assuming here your timed images are for animation.

// start
if (renderLoop==-1) renderLoop = UiApplication.getUiApplication().invokeLater( this, 50, true );

// stop
if (renderLoop!=-1) 
{
  UiApplication.getUiApplication().cancelInvokeLater( renderLoop );
  renderLoop = -1;
}

//assumes your screen implements Runnable
public void run() {
   // do something cool
}

Upvotes: 1

JBM
JBM

Reputation: 2962

Blackberry's Timer is very cheesy - it's just like a Runnable with Thread.sleep() inside. Very commonly for Blackberry, it contains lot of crap you don't actually need and doesn't contain things you do need.

I would dump the Timer and make a class specially for my needs:

abstract public class MyTimer extends Thread {
    private final Object waitobj = new Object();
    private volatile boolean running;
    private volatile boolean canceled;
    private final long due;

    public MyTimer setDelay(long delay) {
        long cur = System.currentTimeMillis();
        due = cur + delay;
        return this;
    }

    public MyTimer setAlarmTime(long dueTimeMillis) {
        due = dueTimeMillis;
        return this;
    }

    synchronized void setIsRunning(boolean running) {
        this.running = running;
    }

    synchronized public boolean isRunning() {
        return running;
    }

    synchronized public void cancel() {
        synchronized (waitobj) {
            canceled = true;
            waitobj.notify();
        }
    }

    public void run() {
        setIsRunning(true);

        long cur = System.currentTimeMillis();
        long sleep = due - cur; 
        while (sleep > 0) {
            synchronized (waitobj) {
                waitobj.wait(sleep);
            }

            if (isCanceled()) return;
            cur = System.currentTimeMillis();
            sleep = due - cur; 
        }
        alarm();

        setIsRunning(false);
    }

    private boolean isCanceled() {
        return canceled;
    }

    abstract void alarm();
}

Then I would invoke it like this:

timer = new MyTimer() {
        void alarm() {
            // do cool things
        }
    };
timer.setDelay(10000).start();

If I need to cancel it I would do it like this:

if (timer.isRunning()) {
    timer.cancel();
}

or simply

timer.cancel();

PS: Note volatile and synchronized things in MyTimer class.

Upvotes: 1

GoRoS
GoRoS

Reputation: 5375

Assuming that you're programming in Java with Swing, you have a method called isRunning() within the Timer Class.

http://download.oracle.com/javase/7/docs/api/javax/swing/Timer.html#isRunning%28%29

Regards

Upvotes: 0

Related Questions