Binh Vova
Binh Vova

Reputation: 133

Blackberry - How to make a clock count down and display on screen

I am making a quiz application with question and answer. I want to make an clock count down ( Minute + Second ) and display it on screen. But I can not how to do that. Can anyone help me please ?

Upvotes: 2

Views: 1374

Answers (2)

Govindarao Kondala
Govindarao Kondala

Reputation: 2862

this is one solution to print time mm:ss format (Minutes:seconds)

   public class StartUp extends UiApplication{
        public static void main(String[] args) {
            StartUp start=new StartUp();
            start.enterEventDispatcher();
        }
        public StartUp() {
            pushScreen(new Timerscreen());
        }
    }

          class Timerscreen extends MainScreen
        {
            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);
            }
        }

Upvotes: 3

alishaik786
alishaik786

Reputation: 3726

Same like above but some change:

First set:

int secs=120; // how many seconds you want;

public void callTheTimer()
{   
    label=new LabelField("\t");
    add(label);
    timer=new Timer();
    timerTask=new TimerTask() 
    {
        public void run() 
        {
            synchronized (UiApplication.getEventLock()) 
            {
                if(secs!=0)
                {
                    label.setText(""+(secs--)+" secs");                 
                }
                else
                {   
                    timer.cancel();                     
                    UiApplication.getUiApplication().invokeLater(new Runnable() 
                    {
                        public void run() 
                        {
                            label.setText("");
                            Dialog.alert("Times Up");
                            secs=120;                               
                        }           
                    });                     
                }
            }
        }
    };
    timer.schedule(timerTask, 0, 1000);
}

This is enough;

Upvotes: 0

Related Questions