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.scheduleAtFixedRate(almTsk, 0, s*1000); //sample at 30s
timeNow=almTsk.TimeNow();
System.out.print(timeNow[0]);
System.out.print(timeNow[1]);
if(timeNow[0]==h && timeNow[1]==m){
System.out.print("Alarm");
StopAlarm();
}
}
catch (IllegalArgumentException e1){
System.out.println("delay is negative");
}
catch (IllegalStateException e2){
System.out.println("task was already scheduled or cancelled, timer was cancelled");
}
}
public void StopAlarm (){
timer.cancel();
}
}
import java.util.Calendar;
import java.util.TimerTask;
public class AlarmTask extends TimerTask{
private int[] timeNow;
private Calendar cal;
public AlarmTask(){
timeNow=new int[2];
cal=Calendar.getInstance();
//System.out.print(cal.get(Calendar.HOUR_OF_DAY));
}
public void run(){ //sample of time
timeNow[0]=getHour();
timeNow[1]=getMinute();
}
public int getHour(){
return cal.get(Calendar.HOUR_OF_DAY);
}
public int getMinute(){
return cal.get(Calendar.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]