Wednesday, June 5, 2013

Determine Inactivity and Activity Event for Adobe AIR Application

A FlexEvent.IDLE event will dispatch every 100 milliseconds when there has been no keyboard or mouse activity for 1 second. IDLE needs to be bound to a SystemManager instance and we will need to reference the mx_internal namespace later on:

1
2
3
import mx.managers.SystemManager;
import mx.events.FlexEvent;
import mx.core.mx_internal;

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       backgroundColor="#FFFFFF"
                       top="0"  bottom="0"
                       width="100%" height="100%"
                       showStatusBar="false"
                       preinitialize="preinitializeHandler(event)"
                       creationComplete="init(event)">

<fx:Script>
        <![CDATA[

             import mx.events.FlexEvent;
             /**
             * METHOD TO DETECT USER IDLE TIME AND SWITCH THE VIEWS
             **/
            public function initIdleExampleApp():void {
                systemManager.addEventListener(FlexEvent.IDLE,onUserIdle,false,0,true);
                systemManager.addEventListener(MouseEvent.CLICK,onUserActive,false,0,true);
                systemManager.addEventListener(MouseEvent.MOUSE_MOVE,onUserActive,false,0,true);
                systemManager.addEventListener(KeyboardEvent.KEY_DOWN,onUserActive,false,0,true);
                systemManager.addEventListener(KeyboardEvent.KEY_UP,onUserActive,false,0,true);
                systemManager.addEventListener(MouseEvent.MOUSE_WHEEL,onUserActive,false,0,true);
            }
            /**
             * METHOD TO HANDLE USER ACTIVE STATE AND SWITCH TO HOME SCREEN
             **/
            public function onUserActive(event:Event):void {

                       trace("User in active state ");
                      // TODO your logic here

               }
            /**
             * METHOD TO HANDLE THE USER IDLE TIME AND CHANGE THE VIEW TO SLIDE SHOW
             **/
            public function onUserIdle(event:Event):void {
                    if ( event.currentTarget.mx_internal::idleCounter ==1000 )
                    {
                        event.currentTarget.mx_internal::idleCounter = 0;

                          // TODO your logic here                                            
                      }
            }

            /**
             * METHOD TO INITIALIZE THE PRE-REQUISITIES
             **/
            protected function init(event:FlexEvent):void {
                 initIdleExampleApp();
            }

    ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
  </s:WindowedApplication>

No comments:

Post a Comment