Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
vivart
Posts: 147
Registered: ‎08-27-2010
My Device: 9800

Timertask

My application is running in background. I want to create a TimerTask that will run everyday at night 12.

 

How to do that??

Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Timertask

[ Edited ]

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.

Please use plain text.
Developer
simon_hain
Posts: 16,216
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Timertask

i would suggest http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/system/ApplicationManager.html... long, boolean)

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
vivart
Posts: 147
Registered: ‎08-27-2010
My Device: 9800

Re: Timertask

[ Edited ]

@

 

 

		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);

 

 

 

Please use plain text.
Developer
simon_hain
Posts: 16,216
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Timertask

maybe http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/system/RealtimeClockListener.h...

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Timertask

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.

Please use plain text.
Developer
vivart
Posts: 147
Registered: ‎08-27-2010
My Device: 9800

Re: Timertask

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.

 

Please use plain text.
Developer
sannyo
Posts: 148
Registered: ‎11-16-2009
My Device: Not Specified

Re: Timertask

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.

 

Please use plain text.
Developer
vivart
Posts: 147
Registered: ‎08-27-2010
My Device: 9800

Re: Timertask

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.

Please use plain text.
Developer
simon_hain
Posts: 16,216
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Timertask

if you do only light processing (like some checks) implementing the realtime listener should be no problem.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.