I have a flex application which runs on a touch screen and I want to have the application reset itself when no one is using it.
To get this done I created a Timer that would reload my Flex app every 5 minutes and an event listener which would reset the timer everytime someone touched the screen (or clicked the app).
Example below.
AppTimer.as ActionScript Class File
Main.MXML File
package
{
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.net.URLRequest;
import flash.net.navigateToURL;
public class AppTimer
{
// Create Time Object to fire every minute for 5 minutes
public var myTimer:Timer = new Timer(60000,5);
public function AppTimer()
{
// designates listeners for the completion event
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
// starts the timer ticking
myTimer.start();
}
// Function restarts the timer.
public function resetTimer():void
{
myTimer.reset();
myTimer.start();
}
// Function gets called when timer runs out. Reloads the page
public function onTimerComplete(evt:Event):void
{
var ref:URLRequest = new URLRequest("javascript:location.reload(true)");
navigateToURL(ref, "_self");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns="*" creationComplete="initApp()">
<mx:Script>
<![CDATA[
// Creates the AppTimer Object
public var myTimer:AppTimer = new AppTimer();
// Function to run when app is created
private function initApp():void
{
// Add Event listner to track mouse clicks
application.addEventListener(MouseEvent.CLICK, globalClickEvent);
}
// Everytime a user clicks in the applicatoin run this function.
private function globalClickEvent(event:MouseEvent):void
{
myTimer.resetTimer(); // Call timer reset function
}
]]>
</mx:Script>
<mx:Panel width="100%" height="100%" title="My App with Timer"/>
</mx:Application>
If anyone knows a better way to accomplish the same task I would be interested to hear about it.
I'm wondering if theres a better way to restart the app witout using navigateToURL.
ReplyDeleteUnforturnately if the app is embeded in an html page, that approach doesnt work too well.