检测Flex程序运行环境(AIR/浏览器) Detect the Environment (AIR/Browser) Your Flex App Runs On

d3A big pack folderfonts We need to detect the environment that our app runs on so that we can change the UI accordingly. For example, we'll show system menu on AIR but not in browser:

if(SystemUtils.isAirEnvironment()) {
  // show menu
}

Implementation:

package com.insprise.common
{
    import flash.utils.getDefinitionByName;

/**
* Provides system utilities.
*/
public class SystemUtils
{

    private static var _environment:int = 0;
    public static const ENV_AIR:int = 2;
    public static const ENV_BROWSER:int = 1;
 
    /**
     * Checks whether the application is running on AIR.
     */
    public static function isAirEnvironment():Boolean {
        if(_environment == 0) { // detect.
            _environment = ENV_BROWSER; // default.
            try {
                if(getDefinitionByName("mx.core.WindowedApplication") != null) {
                    _environment = ENV_AIR;   
                }
            }catch(e:Error) {
                // possible ReferenceError, ignore.
            }
        }   
        return _environment == ENV_AIR;
    }
 
    /**
     * Checks whether the application is running in browser.
     */
    public static function isBrowserEnvironment():Boolean {
        return ! isAirEnvironment();   
    }

} // end class
} // end package