Farid Farhat
Farid Farhat

Reputation: 2310

Make a notification that will run every day at 9:00 pm

I want to make an auto synchronize in my application that will happen every day at 9:00 pm. I am doing this in the way of :

try { Thread.sleep(24hours); } catch (Exception ex) { }

But the problem is when the user turns off his device before 24 hours, When turning it on the thread will sleep again for 24 hours.

So I am thinking of a way to manage this depending on the clock of the system. When the device has the time of 9:00pm, the application will be alerted and I will do the work.

Is it possible to make this?

Upvotes: 1

Views: 603

Answers (6)

Neifen
Neifen

Reputation: 2595

Maybe you can do it with Calendar:

    boolean nine=false;

    //until it's nine'o clock
    while(!nine){

      Calendar calendar=Calendar.getInstance();
        if(calendar.get(Calendar.HOUR_OF_DAY)==9&&calendar.get(Calendar.MINUTE)==0&&calendar.get(Calendar.SECOND)==0&&calendar.get(Calendar.MILLISECOND)==0){
            nine=true;
                nine=true;
            }
            else {
                sleep(yourTime);
            }
    }

    //...whatever should happen now...

   //put the boolean back to false;
    nine=false;

Upvotes: -1

Michael Donohue
Michael Donohue

Reputation: 11876

You want to schedule your app to run at a particular time. BlackBerry's OS supports this. See Schedule a BlackBerry application

Upvotes: 2

Scott W
Scott W

Reputation: 9872

You could consider using the RealTimeClockListener. You will then write a method clockUpdated() that will be called by the OS every minute. In there you can do your calculation to determine if it is time to execute your code.

Upvotes: 0

Michal Vician
Michal Vician

Reputation: 2545

The problem might be resolved by running simple background thread.

  public static void main(String[] args) {
        Thread myJobThread = new Thread(this, "My background job");
        myJobThread.setDaemon(true);
        myJobThread.start();
    }

    // check the time every minute
    private int checkRate = 60000;

        @Override
        public void run() {
            while(true) {
                Date now = new Date();
                if (now > 9a.m.) {
                    // do your stuff
                }
                try {
                    Thread.sleep(checkRate);
                } catch (InterruptedException e){
                    // handle ex
                }
            }
        }

Upvotes: 0

Asaf
Asaf

Reputation: 825

I recommend using one of the available, open source java schedulers already made: List of Open Source Job Schedulers in Java

The one I'm familiar with is the first in that list: Quartz

... Looks like someone beat me to it. (The answer above was added while I was writing this one)

But just to expand real quick, you can easily set up "jobs" that run in the background, separate from the user. Now I've personally used this with websites and not with devices that users can turn off and on. That being said I'm sure what you're trying to do has been tackled before and you should give quartz a try, it can probably do what you need.

Good luck, -Asaf

Upvotes: 2

smp7d
smp7d

Reputation: 5032

Have you taken a look at Quartz (http://www.quartz-scheduler.org/)? Maybe it will be applicable.

Upvotes: 2

Related Questions