i don' understand why my Alarm is blocked

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puntino
    New Member
    • Mar 2007
    • 4

    i don' understand why my Alarm is blocked

    Hi
    I have developed an Alarm, I haven' problem of runtime or compiletime but the application seems blocked.
    When I call the method AlarmExecute, it runs only the first time ( when i click onto run button of Eclipse), I seen only one time the hour in the console!
    please could anyone help me.
    This the code

    [PHP]import java.util.Timer ;

    public class Alarm {
    private int s;
    private int h;
    private int m;
    private int[]timeNow;
    private Timer timer;
    private AlarmTask almTsk;

    public Alarm (int hour, int min) { //hour, min ->alarm time set by the user
    h=hour;
    m=min;
    s=60;
    timer=new Timer();
    almTsk=new AlarmTask();
    timeNow=new int[2];
    }
    public void AlarmExecute(){
    try{
    timer.scheduleA tFixedRate(almT sk, 0, s*1000); //sample at 30s
    timeNow=almTsk. TimeNow();
    System.out.prin t(timeNow[0]);
    System.out.prin t(timeNow[1]);
    if(timeNow[0]==h && timeNow[1]==m){
    System.out.prin t("Alarm");
    StopAlarm();
    }
    }
    catch (IllegalArgumen tException e1){
    System.out.prin tln("delay is negative");
    }
    catch (IllegalStateEx ception e2){
    System.out.prin tln("task was already scheduled or cancelled, timer was cancelled");
    }
    }
    public void StopAlarm (){
    timer.cancel();
    }

    }


    import java.util.Calen dar;
    import java.util.Timer Task;

    public class AlarmTask extends TimerTask{
    private int[] timeNow;
    private Calendar cal;

    public AlarmTask(){
    timeNow=new int[2];
    cal=Calendar.ge tInstance();
    //System.out.prin t(cal.get(Calen dar.HOUR_OF_DAY ));
    }

    public void run(){ //sample of time
    timeNow[0]=getHour();
    timeNow[1]=getMinute();
    }
    public int getHour(){
    return cal.get(Calenda r.HOUR_OF_DAY);

    }
    public int getMinute(){
    return cal.get(Calenda r.MINUTE);
    }

    public int[]TimeNow(){
    return timeNow;
    }


    }

    ublic class Test {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Alarm a;
    a=new Alarm(23,20);
    a.AlarmExecute( );

    }

    }

    [/PHP]
Working...