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

Reply
Developer
flying_ace
Posts: 21
Registered: ‎12-27-2010
My Device: PlayBook

How to keep timer going when app is minimized

My app has a timer which is started when the user clicks a button. But when the app is minimized, the timer pauses. I would like it to keep going so that a user can minimize the app, perform another task and then return to the app and have the timer be up to date, so to speak.

 

We've seen that videos and songs will keep playing when the apps that control them are minimized. Can this be done for a Timer in an AIR app?

 

Thanks,

 

David

Please use plain text.
Developer
Harry_Dodgson
Posts: 153
Registered: ‎10-26-2010
My Device: Bold 9900

Re: How to keep timer going when app is minimized

Hi,

 

What I think would work for you is to get the current time in milliseconds when your app is minimized, then again when it is resumed.  A simple calculation will find out how long it was 'asleep' and what needs to be done to continue as if it hadn't stopped.

 

Harry

--------------
Accepted PlayBook Applications: Marmi-doos, BASIC, Run and Hide, SuperClip
Accepted OS4-7 SmartPhone App: KeepLightOn
Please use plain text.
Developer
TheDarkIn1978
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook

Re: How to keep timer going when app is minimized

[ Edited ]

interesting, i haven't arrived yet at working with the Activate and Deactivate events for my application, but i was under the impression that we were responsible for stopping active processes during a deactivation in order to save on system resources, that the application would continue running normally in a minimized state.

 

try preventing the default action (a pause of your application) when the application becomes minimized, or try continuing your application from where it would normally become deactivated:

 

 

NativateApplication.nativeApplication.addEventListener(Event.DEACTIVATE, callback)

private function callback(evt:Event):void
     {
     evt.preventDefault();

     //or

     //continue application run
     }

 

 


PlayBook Applications:
Drop Swatch
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: How to keep timer going when app is minimized

David, you seem to be asking for two unrelated things.  You want a "timer" (as in an instance of the Timer class?  or something else?) which is "up to date" when the user returns to the app.  What's that actually mean?  The system date, for example, will certainly be up to date.  A periodic Timer would start firing again when the app was reactivated.  What exactly do you want there?

 

You also say you want behaviour similar to the video player that runs while minimized... but that's an entirely different thing.  It means the app doesn't actually get paused, but keeps running, with full access to the system even while minimized.

 

I don't think the two things are the same, and it's not clear which, or what, you actually want.


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!
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: How to keep timer going when app is minimized

@peter: its clear David wants a function of his app to continually run in the background no matter what state his app is in (activated or deactivated).

 

@david: i think its best to take what harry and darkin said. since ur timer starts and keeps going and stops when it is deactivated, what you can do is listen for the deactivate event. then record the time when the app was deactivated and then onces the app is reactivated record the current time. subtract the two numbers and you'll get the total amount of time your app was deactivated and just add that time to the timer counter and it should be "up to date".

 

its not real time but manipulating the timer in such a way with events can get you the effect. good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: How to keep timer going when app is minimized

 


JRab wrote:

@peter: its clear David wants a function of his app to continually run in the background no matter what state his app is in (activated or deactivated).


 

Thanks, Joynal, but when he says "... a user can minimize the app, perform another task and then return to the app and have the timer be up to date, so to speak." it most certainly is not clear (to me) that he expects dynamic behaviour from his app while minimized

 

I agree that's a likely interpretation.  Just not a certain one.  As I've said, I try not to read too much into what people really want, after many years of assisting with requirements definition, problem reports, and such.  (In this case, on the balance of probabilities I agree you're likely right about the interpretation, but that doesn't mean my comments won't help David be clearer in future posts, saving him and all of us wasted time.)


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!
Please use plain text.
Developer
TheDarkIn1978
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook

Re: How to keep timer going when app is minimized

i just ran the following code thru the simulator and the application did not pause when it was minimized or when i switched to another application.

 

the debugger continued to trace random numbers from 0 to 10k, albeit a touch slower when the application was deactivated, but i feel that might have something to do with the simulator.

 

 

 

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class PlayBookTest extends Sprite
	{
		public function PlayBookTest()
		{
			init();
		}
		
		private function init():void
		{
			stage.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
		}
		
		private function enterFrameEventHandler(evt:Event):void
		{
			trace(Math.random() * 10000);
		}
	}
}

 

 


PlayBook Applications:
Drop Swatch
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: How to keep timer going when app is minimized

hey peter,

 

sorry didnt mean to make it sound like you werent helping. was justs stating the OP's intention. although i agree that the OP could have been more clear (a lot of times they could be) but sometimes its difficult for them to convey what they want. and if we're wrong in assuming what they want im sure they'll reply with a correction.

 

but i absolutely agree that your post will help them convey their message better in the future.

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: How to keep timer going when app is minimized

[ Edited ]

TheDarkIn1978 wrote:

... albeit a touch slower when the application was deactivated, but i feel that might have something to do with the simulator.


Kudos for the quick test.  By the way, I've read recently (here's a similar article though it's not the one I read) that applications that are deactivated have their frame rate decreased to about 3 frames per second.  Presumably there will be APIs to modify that behaviour, but of course nothing official has been said about it yet.  Anyway, that could explain the apparent decrease in update rate that you saw.


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!
Please use plain text.
Developer
flying_ace
Posts: 21
Registered: ‎12-27-2010
My Device: PlayBook

Re: How to keep timer going when app is minimized

Thanks to all for the thoughtful replies.

 

To clarify, what I am referring to in my original post as "a timer" was an instance of the Timer class. The Timer is being used as a game timer, like a timer for increasing blinds in Texas Hold 'Em Poker. As such, it's unfortunate that there's no apparent way of keeping the timer going when the app is minimized.

 

The best solution seems to be to catch the current time in miliseconds using Date() when the app is minimized/deactivated and then to catch the current time in miliseconds again when it is reactivated, and then use the difference to update the Timer. Until a better solution is found, this may be what I'll have to do.

 

Thanks again to all,

 

David

Please use plain text.