Search code examples
androidactionscript-3flashair

Adobe Air - Detect app is in the background without using permissions?


I have created an app using adobe Flash CS6 / Adobe Air 26.0 .

To pause the game whenever the app goes into background, I'm using the Deactivate state correctly, and everything is working fine.

I needed to add permission through the xml file to read the phone state READ_PHONE_STATE.

I understand that this permission also implies that the app is reading the phone's phone number, log and several other stuff, that's why I had to create a privacy policy.

Is there any other permission that I could use instead of READ_PHONE_STATE since I only need to know if the app is going to the background, and I don't need all the other stuff?


Solution

  • In order to detect if your app went to the background or came back to the foreground you would have to use the READ_PHONE_STATE permission. This permission is added automatically by Adobe AIR when you use the following events.

    Detecting it is done by adding event listeners for deactivating and re-activating the app:

    NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onDeactivate);
    NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, onActivate);
    
    function onDeactivate(evt:Event):void
    {
       // Pause the game
    }
    function onActivate(evt:Event):void
    {
       // Un-Pause the game
    }
    
    • The Event.DEACTIVATE event is fired when the app goes to the background.
    • The Event.ACTIVATE event is fired when the app goes to the foreground.

    The READ_PHONE_STATE is the most common permission and is used by most apps today.

    The big advantage of Adobe AIR is that it runs the same on all mobile OS. The code above will let you know when a user exits or minimizes the app without the need of permissions for both Android and iOS devices.