Actionscript 3 code for quitting an Android App

Or I could have titled this article, The code that drove me crazy this week.

While working on a very simple shooter app using Flash CS5 and exporting to AIR for Android, I noticed that after adding music, then quitting the app, the music still played. I didn’t realize I was making a background-running audio app, but I had. So the Android app wasn’t actually quitting at all, duh, but was instead just getting minimized basically.  Ammo and stuff like that in the game, didn’t get reset when relaunching it. So if any of you have a similar problem, you might be googling stuff like…

Flash cs5, Android, quit application, reset app, audio playing on exit, app not exiting,

…and you’ve come to the right spot. Here’s the code to make that pesky little Flash-made Android App actually quit when you exit or hit the Home button…

if(Capabilities.cpuArchitecture==”ARM”){

NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate, false, 0, true); NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true); NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys, false, 0, true);

}

function handleActivate(event:Event):void

{

NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;

}

function handleDeactivate(event:Event):void

{

NativeApplication.nativeApplication.exit();

}

function handleKeys(event:KeyboardEvent):void

{

if(event.keyCode == Keyboard.BACK) {

NativeApplication.nativeApplication.exit();

}

}

 

Advertisements

4 thoughts on “Actionscript 3 code for quitting an Android App

  1. Eddie says:

    I think you need to differentiate between the user ‘quitting’ the app and ‘pausing’ it to go and do something else (ie run another app/phone call etc) before coming back to the app to carry on where they left off….

  2. Thanks for figuring this code out, I wasn’t sure how to do it. I made a game last night that utilizes the mic to create movie clips and it made my roommates phone give terrible feedback from the echo. When you closed the app the sound continued so he had to uninstall it to get it to stop. setUseEchoSuppression didn’t seem to help at all either.

  3. jesliphius says:

    pausing happens automatically… this is perfectly titled. it allows an AUTO QUICK when the app is left. (doesn’t allow for pause)…. You can assign the event to a custom button in the app if you want a close app button.

    great post! thx for info

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s