12-08-2010 04:50 AM
My application is running in background. I want to create a TimerTask that will run everyday at night 12.
How to do that??
12-08-2010 05:04 AM - edited 12-08-2010 05:06 AM
Create a Thread which on every minut will Match the date for Night 12AM.
--------------------------------------------------
Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.
12-08-2010 05:06 AM
i would suggest http://www.blackberry.com/developers/docs/6.0.0api
12-08-2010 05:18 AM - edited 12-08-2010 05:21 AM
@simon_hain At first i was also thinking for that but its not intercessory that my application will run every time and when its not running there is no need to run the task.
@nitinverma274 currently i am trying simillar thing instead of thread i am using timertask that will poll after some interval and check the current time. But when i changing time of simulator its not invoking.
here is my code
timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Calendar c = Calendar.getInstance();
c.set(Calendar.AM_PM, Calendar.PM);
c.set(Calendar.HOUR, 11);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
Calendar now = Calendar.getInstance();
if(now.after(c)){
//do some task
}
}
};
timer.scheduleAtFixedRate(task, 0, 3000);
12-08-2010 05:32 AM
12-08-2010 05:56 AM
I think Simon is right.
You need to Implement RealtimeClockListener.
--------------------------------------------------
Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.
12-08-2010 06:14 AM
In my previous code i was doing a mistake i was calculating fixed time also in run method. I have changed my code
final Calendar c = Calendar.getInstance();
c.set(Calendar.AM_PM, Calendar.PM);
c.set(Calendar.HOUR, 11);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Calendar now = Calendar.getInstance();
if(now.after(c)){
}
}
};
timer.scheduleAtFixedRate(task, 0, 3000);
Now Simon's method is also working and mine also. So i am confused which one to use. in Timertask i can change that polling interval so i think i will go for that.
12-09-2010 05:38 AM
Hi
maybe you can use
public void schedule(TimerTask task,Date time)
instead checking the current date every 3 seconds or every minutes.
When you start you can check the current date and set up the date object for the scheduler.
You can listen with RealtimeClockListener - as Simon suggested - to the time changes and reinitialise the timer if the user change their time (or do what you need to do). If the timer executed then in the end of the timertask you can set up the timer for the next day.
12-09-2010 05:49 AM
yeah i have tried that method also.
1. It was working fine but if you change the date of device manually this method will not be updated because form javadocs "it schedules tasks using the Object.wait(long) method.
2. second method that you suggested with RealtimeClockListener
that also i have tried but the problem in RealtimeClockListener is "Note that the system actually invokes this method each minute." from javadoc.
So actually its working like timertask with i minute delay.
Andvantage of RealtimeClockListener is if user changes date i will get instant notification.
but if i am using timertask i can change polling time.
12-09-2010 06:50 AM
if you do only light processing (like some checks) implementing the realtime listener should be no problem.