Display an alert when the event is 5 minutes away

Asked

Viewed 350 times

1

I am developing an agenda in Java, where I have in the database the date and time saved. However, what would be the best way to display an alert 5 minutes before the event? Do I have to run the query method all the time until the times come? Or is there a simpler way to do this?

  • would not be better a notification or even want an Alert?

  • can be a notification too. The problem is how I keep checking the time. I will have to make a method to search the bank all the time?

1 answer

1


Techies

You can use a Timer (java.util.Timer) and schedule it to run 5 minutes before your time:

private Timer timer;

private void programaPara(Date data){// mande a data com 5 minutos de antecedência.

       timer = new Timer();

       timer.schedule(new TimerTask() {

           @Override
           public void run() {
               //faça algo
           }
      }, data);
   }

To subtract 5 minutes from the date you may be using the Calendar:

public Date getDataMenos5Minutos(Date data){
     Calendar calendar =Calendar.getInstance();
     calendar.setTime(data);
     calendar.add(Calendar.MINUTE, -5);

     return calendar.getTime();
}
  • 1

    Thanks man, I did a task that does a check on my list of 1 in 1 minute

  • 1

    Oops. I already made a similar application. In my case I searched all of the bank for a certain day, and then created a task alert to each found, and then when the time was task performed the action of my class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.