AS3: Blackberry Playbook Boilerplate


/ Published in: ActionScript 3
Save to your folder(s)

It seems that every time I work on a Blackberry app, by default I use some boilerplate code like this. The idea is simply to handle the background events whenever a user flips to another application.


Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.desktop.NativeApplication;
  4. import flash.desktop.SystemIdleMode;
  5. import flash.display.Sprite;
  6. import flash.display.StageAlign;
  7. import flash.display.StageScaleMode;
  8. import flash.events.*;
  9. import flash.system.Capabilities;
  10.  
  11. [SWF(width="1024", height="600", backgroundColor="#f1f1f1", frameRate="24")]
  12. public class Main extends Sprite
  13. {
  14. private static const FRAME_RATE_DEFAULT:Number = 24;
  15. private static const FRAME_RATE_STANDBY:Number = 4
  16.  
  17. public function Main()
  18. {
  19. stage.align = StageAlign.TOP_LEFT;
  20. stage.scaleMode = StageScaleMode.NO_SCALE;
  21. stage.addEventListener(Event.ACTIVATE, onActivateHandler, false, 0, true );
  22. stage.addEventListener(Event.DEACTIVATE, onDeactivateHandler, false, 0, true );
  23. addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true );
  24.  
  25. NativeApplication.nativeApplication.addEventListener(Event.EXITING, onExitHandler, false, 0, true );
  26.  
  27. checkEnvironment()
  28. }
  29.  
  30. private function checkEnvironment():void
  31. {
  32. if (Capabilities.manufacturer.indexOf('VMware') == -1) {
  33. //It is a BlackBerry PlayBook Tablet.
  34. } else {
  35. //It is a BlackBerry PlayBook Simulator.
  36. }
  37. }
  38.  
  39. private function init( e:Event ):void
  40. {
  41. //Self Destruct Handler
  42. e.currentTarget.removeEventListener(e.type, arguments.callee );
  43. }
  44.  
  45. private function onExitHandler( e:Event ):void
  46. {
  47. NativeApplication.nativeApplication.exit(0);
  48. }
  49.  
  50. private function onActivateHandler( e:Event ):void
  51. {
  52. //trace( "Main::onActivateHandler:" );
  53. stage.frameRate = FRAME_RATE_DEFAULT;
  54. NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
  55. }
  56.  
  57. private function onDeactivateHandler( e:Event ):void
  58. {
  59. stage.frameRate = FRAME_RATE_STANDBY;
  60. NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
  61. }
  62. }
  63. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.