Binh Vova
Binh Vova

Reputation: 133

How to display clock count down whole the application on Blackberry?

I make and display an clock count down with this code

LabelField time;
            long mille=0;
            Timer timer=null;TimerTask task=null;
            public Timerscreen() {
                mille=1000*60*1;
                time=new LabelField();
                add(time);
                timer=new Timer();

                task=new TimerTask() {

                    public void run() {
                        synchronized (UiApplication.getEventLock()) {

                            if(mille!=0){
                                SimpleDateFormat date=new SimpleDateFormat("mm:ss") ;
                                System.out.println("================="+date.formatLocal(mille)+"====================="+Thread.activeCount());
                                time.setText(date.formatLocal(mille));
                                mille=mille-1000;
                            }else{
time.setText("00:00");
mille=1000*60*1;
                                timer.cancel();
                                UiApplication.getUiApplication().invokeLater(new Runnable() {
                                    public void run() {
                                        Dialog.inform("Time expaired");
                                    }
                                });

                            }
                        }
                    }
                };
                timer.schedule(task,0, 1000);

And when I push a new screen , I want to this clock still display and count down. How can I do that ?

Upvotes: 4

Views: 523

Answers (1)

Rupak
Rupak

Reputation: 3674

It is not possible to add a single ui field or manager into two managers or screens.. every ui field or manager must have at most one parent (screen or manager).

So if you need a LabelField which will hold and show time on different screens, then you only need to implement some sort of listener which will listen for the time changes.. and for every changes you have to update the screen and the LabelField with the new value. You have already implemented a TimerTask which will provide you updated data.

[Edited - added later]

you can check the following codes, not tested but something like this will solve your problem...

class MyTimerUtil {
    TimerListener listener = null;

    public MyTimerUtil() {      
    }

    public void setTimerListener(TimerListener listener) {
        this.listener = listener;
    }

    public void startTimer() {
        final int interval = 1000;
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                // add your codes..

                // notify others
                if (listener != null) {
                    listener.timeChanged();
                }
            }
        };
        timer.schedule(task, 0, interval);
    }
}


interface TimerListener {
    public void timeChanged();
}

class ScreeA extends MainScreen implements TimerListener {

    public void timeChanged() {
        // add Codes here on time changed event     
    }   
}

in the above snippet, you can implement TimerListener interface in any screen instance and can get update on every time changed event by the MyTimerUtil class. For that, you have to set an instance of ScreeA (which implements TimerListener) via setTimerListener() of the MyTimerUtil class.

Also need to start the timer by calling startTimer() method.

Upvotes: 1

Related Questions