I'm trying to do a task at specific time and start the TimerTask for the first time on a button click. When the button is clicked, after first time the system.out
should be printed everyday at a time. But it works only when button is clicked and not at specified time.
This is the activity in which I set button to start timer.
showViewReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Alarm alarm = new Alarm();
}
});
The class with timer:
public class Alarm {
Timer _timer;
Context context;
public Alarm() {
// Create a Date corresponding to 10:30:00 AM today.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 56);
calendar.set(Calendar.SECOND, 0);
this.context = context;
Date alarmTime = calendar.getTime();
_timer = new Timer();
_timer.schedule(new AlarmTask(), alarmTime);
}
class AlarmTask extends TimerTask {
public void run() {
System.out.println("*************Ok Alarm****************************");
// _timer.cancel();
}
}
}
What am I doing wrong?