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

Adobe AIR Development


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 200
Registered: ‎03-10-2011
My Device: Not Specified
Accepted Solution

Event when time changes?

[ Edited ]

I want to be able to run a function at an exact time specified by the user. The time will always be an exact hour and minute, so I would think I only need to check once a minute. But, I want it to be accurate to happening at the point where the minute changes. For example, if a user sets the time to "4:30" I want the function to run as soon as the clock on the PlayBook changes to 4:30 (preferably as close as possible). But if my timer runs a listening function every minute and it starts in between minutes for example "2:00:30" then it won't know it's "2:01" until it's "2:01:30", exactly one minute after it started.

 

I could run a function on every frame, but I didn't think that that would be the best way of doing it.

EDIT: I guess the obvious solution to some could be to just check once a second, but is this really the most efficient way?

 

Is there some sort of time event listener, or some other way of doing this?

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Event when time changes?

I can think of ways of doing what you want effectively, except in one case you don't cover. 

 

What do you want to happen when the user changes the system time on you?

 

Also, how important is it really to be exactly "on time" (given the rather fluid nature of wall-clock time)?  Maybe your requirement is overly strict.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Trusted Contributor
Posts: 108
Registered: ‎12-28-2010
My Device: Free PlayBook
My Carrier: Verizon

Re: Event when time changes?

This is untested but it might do what you want:

 

import flash.events.TimerEvent;
import flash.utils.Timer;

...

var secondsTimer:Timer = new Timer(1000);	// 1000 milliseconds or a second delay
secondsTimer.addEventListener(TimerEvent.TIMER, onSecondsTimer);
secondsTimer.start();	// start timer

...

secondsTimer.stop();	// stop timer


private function onSecondsTimer(e:TimerEvent):void
{
	var currentTime:Date = new Date();	// get current date/time

	if(currentTime.getHours() == userSetHour && currentTime.getMinutes() == userSetMinute)
	{
		do something
	}

}

 

Highlighted
Developer
Posts: 200
Registered: ‎03-10-2011
My Device: Not Specified

Re: Event when time changes?

[ Edited ]

Maybe I am being a little to strict about it. Smiley Tongue

 

But I think, PBDev that your solution is probably the best right now! Thank you very much!!

Regular Contributor
Posts: 75
Registered: ‎03-21-2011
My Device: None
My Carrier: T-Mobile

Re: Event when time changes?

Mike's solution will work.

 

One thing to keep in mind, that I had to learn the hard way, is that you can't do math with getHours() and getSeconds() and such, because they roll over at 12 and 60 and so on. If you try to, say, subtract 168 hours from the Hours value to go back in time one week, the code will break badly. Instead, use getTime() which returns an absolute value (UNIX time I think?) in milliseconds and then do the math on that.

Developer
Posts: 200
Registered: ‎03-10-2011
My Device: Not Specified

Re: Event when time changes?

Oh ok, thanks for that, knowing this could save me a lot of time!