Developing Android Applications with Adobe AIR [27]
By default, your application occupies the screen underneath the 38-pixel-tall status bar that displays information such as network signal, battery life, time, and notifications. If you wish to hide this bar and go full screen, in Flash Professional select Full Screen under File→AIR Android settings→General. In Flash Builder, add the following node as a child of the Closing the Application import flash.desktop.NativeApplication; import flash.events.Event; NativeApplication.nativeApplication.addEventListener(Event.EXITING, onExit); function onExit(event:Event):void { // save application's current state } However, the Android OS may choose to terminate background processes immediately if RAM is needed. In some cases, such as receiving a phone call, there is no guarantee that NativeApplication will have a chance to dispatch the exiting event before being shut down. It is therefore a good practice to save critical data often while the application is active. We will discuss how to save data locally later in this chapter. The Android OS closes applications that were inactive the longest first. If you want to immediately shut down a particular application, select Settings→Applications→Manage applications→Running, click the desired icon, and press “Force stop.” You can also use third-party task killer applications, which conveniently display all the applications currently running in one selection. The Android UX guidelines recommend using the back or home button as an exit button. But you may choose to shut down your AIR application via code when it moves to the background, or make it a user option while it is running to run the following method: NativeApplication.nativeApplication.exit(); Moving Between the Background and Foreground NativeApplication.nativeApplication.addEventListener (Event.ACTIVATE, onActivate); NativeApplication.nativeApplication.addEventListener (Event.DEACTIVATE, onDeactivate); Improvements were made in Flash Player 10.1 and AIR 2.0 to conserve memory and battery life. When an application is no longer active, it goes into sleep mode, it stops rendering, and its frame rate drops to 4 fps. This number was chosen because socket connections and the like tend to break at lower frame rates. If AIR doesn’t have any persistent connections, you can go as low as 0 fps. The command to change the frame rate is: stage.frameRate = 0; Some items are kept active. For instance, audio keeps playing, video with or without an audio track continues running, sensor listeners such as those for the accelerometer and geolocation are not removed, and timers keep running. Listen to Event.DEACTIVATE and change the default behavior as needed when receiving the event. This is not done automatically, so you are responsible for managing this. The following code shows how a video plays and pauses whenever the application is in the foreground or background: import flash.desktop.NativeApplication; import flash.events.Event; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; var stream:NetStream; NativeApplication.nativeApplication.addEventListener (Event.ACTIVATE, onActivate); NativeApplication.nativeApplication.addEventListener (Event.DEACTIVATE, onDeActivate); var video:Video = new Video(640, 270); var nc:NetConnection = new NetConnection(); nc.connect(null); stream = new NetStream(nc); stream.client = {}; video.attachNetStream(ns);
If another application is selected, yours is moved to the background but continues to play and does not close. Typically, NativeApplication dispatches an exiting event when it starts the closing process. You can register for that event and be prepared to act quickly according to your application needs:
NativeApplication dispatches an event when it moves to the background. It also dispatches an event when it becomes the active application again. Set listeners if you wish to react to these events: