cieco1109
cieco1109

Reputation: 73

Why TimerTask is running twice?

I want to display a notification at a specific time for my android application. For example: at 3 pm print the notification message. I use this code:

private void checkNotificationStatus() {
     timer.schedule(new TimerTask() {
         @Override
         public void run() {
             addNotificationReminder();
         }
      }, new Date(appointment.getTimeInMillis()));
}

public void addNotificationReminder() {
   System.out.println("i'm here");
}

So, the timer works correctly but the message, "I'm here" is displayed twice instead of just once. If I put a long instead of a date as the second parameter in TimerTask (for example 2000), the message is printed just once. Nonetheless, if I put: appointment.getTimeInMillis() - System.currentTimeMillis() the message is printed twice again.

Any suggestion?

Upvotes: 0

Views: 1252

Answers (1)

NiceWaffel
NiceWaffel

Reputation: 83

The checkNotificationStatus() is probably called two times. Maybe put a println() before timer.shedule() to check, whether the method is called multiple times or whether it really is the timers fault.

Upvotes: 2

Related Questions