2

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 ?

Binh Vova
  • 133
  • 1
  • 7
  • hi please give some more information what you need exactly? do you want to count time between question and answer? – Govindarao Kondala Dec 16 '11 at 04:29
  • @HelpMeToHelpYou the Quiz Game have a time limit(ex 2 minute). I want to creat a clock count down 2 minute 0 s to 0 minute 0s. When time finish, quiz is finished. And display clock on my quiz screen ( example image http://pastormikelandry.files.wordpress.com/2010/02/countdown-clock1.jpg ) – Binh Vova Dec 16 '11 at 04:52
  • hi if you have doubts reqarding this come this room http://chat.stackoverflow.com/rooms/4014/knowledge-sharing-center-for-blackberry-and-java – Govindarao Kondala Dec 16 '11 at 05:04
  • @HelpMeToHelpYou I need 20 reputation to talk there . I have not enough. :) – Binh Vova Dec 16 '11 at 06:06
  • @HelpMeToHelpYou There some errors . I am trying to fix error your code. – Binh Vova Dec 16 '11 at 07:13

2 Answers2

3

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);
            }
        }
Govindarao Kondala
  • 2,862
  • 17
  • 27
  • I add your code to my app but seem it have some error. Can you check it again ? – Binh Vova Dec 16 '11 at 07:17
  • I make an new application with only one Myscreen extends MainScreen. and I add your code into MyScreen constructor there have 4 error . Cannot refer to a non-final variable mille inside an inner class defined in a different method. ( at if(mille!=0 ) – Binh Vova Dec 16 '11 at 07:26
  • yeah that run OK. Thanks you a lot – Binh Vova Dec 16 '11 at 07:38
  • I make an VerticalFieldManager and add some button first. Then I want to add this clock. How can I add and set position for this clock ? – Binh Vova Dec 16 '11 at 08:02
  • I known how to do that, sorry. :). – Binh Vova Dec 16 '11 at 08:06
  • add image what is exact your requirement ? i will give you suggestion – Govindarao Kondala Dec 16 '11 at 08:08
  • Hi, i understood it. Thanks HelpMeToHelpYou – Binh Vova Dec 16 '11 at 08:31
  • I have a problem. I am making an Quiz with question and click CheckBoxField to tick answer. And when I click "next button" to go to next question. this clock reset like as beginning but I want it still run. How can do it to clock still run when I push an new screen ? – Binh Vova Dec 16 '11 at 08:43
  • I want to display this clock all sceen in my app and it still run count down when I push new screen. Sorry if I make you trouble. – Binh Vova Dec 16 '11 at 08:51
  • no probs but ask as new question i will send you as new answer please because this code may be helpfull to others hope you can understand – Govindarao Kondala Dec 16 '11 at 08:59
0

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;

alishaik786
  • 3,696
  • 2
  • 17
  • 25
  • I want to display this clock all sceen in my app and it still run count down when I push new screen. can I ? – Binh Vova Dec 16 '11 at 08:56
  • Before push you have to check the condition that if(timer!=null) timer.close(); – alishaik786 Dec 16 '11 at 09:05
  • In the 2nd screen I call method callTheTimer() and it display a clock like as begining. I do not want that. I want clock still count down when clock run from 1st screen. Sorry for my bad English. Hope you understand what I said. – Binh Vova Dec 16 '11 at 09:15
  • Means you want the countDown from starting the Quiz to ending the Quiz. Or Fresh CountDown start for second screen? Now you have the permission to come on chat room name is: "Knowledge Sharing Center For Blackberry and Java" – alishaik786 Dec 16 '11 at 09:28
  • countDown from starting the Quiz to ending the Quiz. And in my Quiz have a lot of screen. I want this clock **display** all screen. How can I do that ? – Binh Vova Dec 16 '11 at 09:32