1
I intend to develop an app that when setting a certain time, schedule a task with timertask to be executed 50 minutes later and soon after that 50 minutes, repeat by 50 minutes in a row. But it turns out that after 50 minutes of scheduled time, the task is not executed. But it turns out that tested with shorter times like 5 minutes, the task performs. Does timertask not accept long times? Here’s an example of what my code might be:
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
timer.schedule(timerTask, 300000, 300000);
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
//use a handler to run a toast that shows the current timestamp
handler.post(new Runnable() {
public void run() {
showNotification();
}
});
}
};
}
For this type of range, it is recommended to use the
AlarmManager
registering an Intent. I think here in the OS has several questions and answers on the subject.– Wakim
@Wakim happened to have used Alarmmanager for this case and somehow the result was "crazy". There were times when I respected the intended interval but other times I didn’t ... Maybe I was using it wrong, but I don’t believe it ...
– C-lio Garcia
I only see this problem if the device is turned off, then you need to register again. I do not know if you ended up calling the
setInexactRepeating
orset
, but in version 19, it does not run at the exact time, but sometime after. This is done to save battery. It is worth taking a look at the documentation: http://developer.android.com/reference/android/app/AlarmManager.html. If you need an exact time window, you can use thesetWindow
(19+): http://developer.android.com/reference/android/app/AlarmManager.html#setWindow(int, long, long, android.app.Pendingintent).– Wakim
@Wakim, I’ll take a look at the documentation and try the setWindow ... Thanks for the help.
– C-lio Garcia