Developing Android Applications with Adobe AIR [28]
addChild(video);
stream.play("yourMovie.mp4");
function onDeActivate(event:Event):void {
stream.pause();
}
function onActivate(event:Event):void {
stream.resume();
}
When a user receives a phone call, allowing your audio to play over the conversation would equate to a poor user experience. Add the READ_PHONE_STATE permission to your application descriptor file. It allows AIR to mute audio while the call is in progress:
When the application goes to the background, take the opportunity to trigger the garbage collector. It is not recommended that you use this too often because it takes time and can affect performance, but a deactivate state may be a natural break point:
system.gc();
NOTE
Information on battery life and free memory is not currently exposed in AIR for Android. Let’s hope it is in a future release. It is available for the RIM Playbook to monitor the state of the device.
Setting Back, Menu, and Search Buttons
The system uses the buttons at the base of your device for navigation and search functions. Except for the home button, you can override their default behavior to use them in your AIR application.
Note that AIR currently cannot communicate with the native user interface and does not have access to the Options menu triggered by pressing the Menu key. The Options menu can hold up to six items; if additional items are stored, it displays a More menu item which reveals an expanded menu. If you want to create a native-like look and feel, you can design a similar-looking menu in your application.
Register for a keyboardEvent.KEY_DOWN event and track which soft key was pressed by identifying its keyCode. Call event.preventDefault() to catch the event before it triggers the default navigation and replace it with your desired behavior:
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
function onKey(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.BACK:
event.preventDefault();
trace("go back within the AIR application");
break;
case Keyboard.MENU:
event.preventDefault();
trace("display a custom menu");
break;
case Keyboard.SEARCH:
event.preventDefault();
trace("perhaps use as a help button");
break;
}
}
No keyboard event is dispatched for the home soft key.
Keep in mind that overriding the back button default behavior is against Android UX guidelines. Some users may give your application a bad review because of it.
A trick I often use is to override these buttons during development to test various cases at runtime. I may, for instance, use the search button to increment a variable, add a listener, or display a benchmarking tool.
Overriding a Dimmed Screen
After your application is idle for a while, the OS makes the device screen go into dimming mode and sets your device to auto-lock if your settings request it. Note that this is different from AIR on the desktop. On the desktop, playing a video at full screen automatically blocks screensavers and power-saving modes. It does not happen automatically on Android.
If you wish to override this behavior during a certain activity, you need to set the following permissions:
For convenience, place this code in your document class or where you first set up your application settings. Import the needed classes:
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
Tell the native application to stay awake:
NativeApplication.nativeApplication.systemIdleMode
= SystemIdleMode.KEEP_AWAKE;
To set the native application to normal behavior again, use this code:
NativeApplication.nativeApplication.systemIdleMode
= SystemIdleMode.NORMAL;
Keep in mind that blocking the idle behavior has a negative impact on battery life. Test it and use it with caution.
Why and How to Save Data
As I mentioned previously,