从flex传递参数到AIR

时间:2013-09-02 07:37:43

标签: flex air flex4

我需要将自定义参数从flex文件传递到AIR文件,

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="AIRInstallBadge()">

    <fx:Script>
        <![CDATA[
            import adobe.utils.ProductManager;
            import flash.system.Capabilities;
            import mx.controls.Alert;

            // Constants:
            public static const AIR_SWF_URL:String = "http://airdownload.adobe.com/air/browserapi/air.swf";
            public static const VALID_PROTOCOLS:Array = ["http","https"];
            // Private Properties:
            // parameters:
            protected var airVersion:String;
            protected var appInstallArg:Array;
            protected var appLaunchArg:Array;
            protected var appID:String;
            protected var appName:String;
            protected var appURL:String;
            protected var appVersion:String;

            protected var pubID:String;
            protected var skipTransition:Boolean;
            //
            protected var installedAIRVersion:String;
            protected var airSWFLoader:Loader;
            protected var airSWF:Object;
            protected var action:String;
            protected var prevAction:String;
            protected var timer:Timer;
            protected var productManager:ProductManager;

            // Initialization:
            public function AIRInstallBadge():void {
                configUI();


                // set up the timer that will be used to check for installation progress:
                timer = new Timer(10000,0);
                timer.addEventListener(TimerEvent.TIMER,handleTimer);

                // set up a product manager for AIR:
                productManager = new ProductManager('airappinstaller' );

                // read params (except strings) from FlashVars:
                var params:Object;
                airVersion = "3.1";
                appInstallArg = ["installed from web"];
                appLaunchArg =  ["launched from web", "hello"];
                appID = "com.ScShare";
                appName = "ScShare";
                appURL = "http://localhost:9292/interfacefinal_New_1-debug/ScShare.air";
                appVersion ="1.0.0";
                pubID = "";
                skipTransition = false;


                // verify all required params are accounted for:
                if (!verifyParams()) {

                    actionFld.text = "";
                    return;
                }

                // strip tags out of the appName:
                appName = appName.replace(/(<.*?>|<)/g,"");



                // load the AIR proxy swf:
                airSWFLoader = new Loader();
                airSWFLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,handleAIRSWFError);
                airSWFLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleAIRSWFInit);
                try {
                    airSWFLoader.load(new URLRequest(AIR_SWF_URL))//, loaderContext);
                } catch (e:*) {
                    handleAIRSWFError(null);
                }
            }

            // Public Methods:
            // Protected Methods:


            // called when there is an error loading the airSWF
            protected function handleAIRSWFError(evt:IOErrorEvent):void {

                actionFld.text = "";
            }

            // called when the airSWF loads and inits
            protected function handleAIRSWFInit(evt:Event):void {
                airSWF = airSWFLoader.content;
                if (airSWF.getStatus() == "unavailable") {

                    return;
                }
                var version:String = null;
                if (appID && pubID) {
                    // check if the application is already installed:
                    try {
                        airSWF.getApplicationVersion(appID, pubID, appVersionCallback);
                        return;
                    } catch (e:*) {}
                }
                enableAction("install");

            }

            // callback from the airSWF when requesting application version
            protected function appVersionCallback(version:String):void {
                if (version == null) {
                    // application is not installed
                    enableAction("install");
                } else if (appVersion && (checkVersion(appVersion,version)==1)) {
                    // old version is installed
                    enableAction("upgrade");
                } else {
                    // current version is probably installed
                    enableAction("launch");
                }

            }

            // handles clicks on the action button
            protected function handleActionClick(evt:MouseEvent):void {
                if (action == "close") {

                    enableAction(prevAction);
                } else if (action == "install" || action == "upgrade" || action == "tryagain") {

                    disableAction();
                    // check if it's installed every 5 seconds:
                    timer.reset();
                    timer.start();
                    airSWF.installApplication(appURL, airVersion, appInstallArg);
                } else if (action == "launch") {
                    airSWF.launchApplication(appID, pubID, appLaunchArg);

                    enableAction("close");
                }
            }

            // triggered  every 5 seconds after installing or upgrading.
            // checks to see if the expected version of the application was successfully installed.
            protected function handleTimer(evt:TimerEvent):void {
                try {
                    airSWF.getApplicationVersion(appID, pubID, tryAgainVersionCallback);
                } catch (e:*) {
                    enableAction("tryagain");
                }
            }

            // call back from version check in handleTimer
            // verifies that version is appVersion, and provides option to launch the app if it is.
            protected function tryAgainVersionCallback(version:String):void {
                if (version != null && (appVersion == null || !(checkVersion(appVersion,version)==1))) {
                    // current version is probably installed
                    timer.stop();
                    enableAction("launch");
                } else {
                    enableAction("tryagain");
                }
            }



            // enables the action button with the appropriate label, and sets the action property
            protected function enableAction(action:String):void {
                if (action == null) {
                    disableAction();
                    actionFld.text = getText("loading");
                    prevAction = null;
                } else {
                    if (this.action != "close") { prevAction = this.action; }
                    actionBtn.addEventListener(MouseEvent.CLICK, handleActionClick);
                    actionBtn.enabled = true;
                    actionFld.alpha = 1;
                    actionFld.text = getText(action);
                }
                this.action = action;
            }

            // disables the action button
            protected function disableAction():void {
                actionBtn.removeEventListener(MouseEvent.CLICK,handleActionClick);
                actionBtn.enabled = false;
                actionFld.alpha = 0.2;
            }



            // return if all required parameters are present, false if not:
            protected function verifyParams():Boolean {
                return !(appName == null || appURL == null || airVersion == null);
            }

            // return null if not a valid URL, only allow HTTP, HTTPS scheme or relative path
            protected function validateURL(url:String):String {
                if (url == null) { return null; }
                var markerIndex:int = url.search(/:|%3a/i);
                if (markerIndex > 0) {
                    var scheme:String = url.substr(0, markerIndex).toLowerCase();
                    if (VALID_PROTOCOLS.indexOf(scheme) == -1) { return null; }
                }
                if (url.indexOf("<") >= 0 || url.indexOf(">") >= 0) {
                    return null;
                }
                return url;
            }

            // returns null if the string is empty or null.
            protected function validateString(str:String):String {
                return (str == null || str.length < 1 || str.indexOf("<") >= 0 || str.indexOf(">") >= 0) ? null : str;
            }

            // returns the specified string from FlashVars (passed as "str_strcode") if available, or the default string if not.
            protected function getText(strCode:String):String {
                var str:String;
                if (str != null && str.length > 1) {
                    return str;
                }
                switch (strCode) {
                    case "error": return "Error!";
                    case "err_params": return "Invalid installer parameters.";
                    case "err_airunavailable": return "Adobe® AIR™ is not available for your system.";
                    case "err_airswf": return "Unable to load the Adobe® AIR™ Browser API swf.";
                    case "loading": return "Loading...";
                    case "install": return "Install Now";
                    case "launch": return "Launch Now";
                    case "upgrade": return "Upgrade Now";
                    case "close": return "Close";
                    case "launching": return "Launching Application";
                    case "launchingtext": return "Please wait while the application launches.";
                    case "installing": return "Installing Application";
                    case "installingtext": return "Please wait while the application installs.";
                    case "tryagain": return "Try Again";

                }
                return "";
            }



            // returns true if the first version number is greater than the second, or false if it is lesser or indeterminate:
            // works with most common versions strings: ex. 1.0.2.27 < 1.0.3.2, 1.0b3 < 1.0b5, 1.0a12 < 1.0b7, 1.0b3 < 1.0
            protected function checkVersion(v1:String,v2:String):int {
                var arr1:Array = v1.replace(/^v/i,"").match(/\d+|[^\.,\d\s]+/ig);
                var arr2:Array = v2.replace(/^v/i,"").match(/\d+|[^\.,\d\s]+/ig);
                var l:uint = Math.max(arr1.length,arr2.length);
                for (var i:uint=0; i<l; i++) {
                    var sub:int = checkSubVersion(arr1[i],arr2[i])
                    if (sub == 0) { continue; }
                    return sub;
                }
                return 0;
            }

            // return 1 if the sub version element v1 is greater than v2, -1 if v2 is greater than v1, and 0 if they are equal
            protected function checkSubVersion(v1:String,v2:String):int {
                v1 = (v1 == null) ? "" : v1.toUpperCase();
                v2 = (v2 == null) ? "" : v2.toUpperCase();

                if (v1 == v2) { return 0; }
                var num1:Number = parseInt(v1);
                var num2:Number = parseInt(v2);
                if (isNaN(num2) && isNaN(num1)) {
                    return (v1 == "") ? 1 : (v2 == "") ? -1 : (v1 > v2) ? 1 : -1;
                }
                else if (isNaN(num2)) { return 1; }
                else if (isNaN(num1)) { return -1; }
                else { return (num1 > num2) ? 1 : -1; }
            }


            // ** this is the public API we expose so the badge configuration application can work with this badge **

            // returns an object containing the basic properties of the badge. The configurator expects minWidth, maxWidth, minHeight and maxHeight.
            public function getProps():Object {
                return {minWidth:215,maxWidth:430,minHeight:180,maxHeight:320};
            }




            // return an object representing the dimensions of the image to export for this badge.
            // This is called when the badge is exported by the configurator so that it can vary dynamically depending on the badge's current dimensions.
            public function getImageSize():Object {
                return {width:205,height:170};
            }

            // handles initial UI setup.
            protected function configUI():void {

                actionFld.text = getText("loading");
                actionFld.mouseEnabled = false;
                disableAction();

            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Label id="imageAltFld" x="27" y="31" width="361" height="26"/>
    <s:Button id="actionBtn" x="27" y="65" width="200" height="32"/>
    <s:Label id="actionFld" x="48" y="97" width="157" height="23" verticalAlign="middle"/>
</s:Application>

AIR文件代码:

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009 xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"width="320" height="276" invoke="onInvokeEvent(event)">


private function onInvokeEvent(event:InvokeEvent):void{
        paramObj = LoaderInfo(this.root.loaderInfo).parameters;         
        //Alert.show(paramObj["myVar"].toString());     

        Alert.show("arguments: "+ event.arguments.toString() + event.currentDirectory.toString());

        var invocation:InvokeEvent = InvokeEvent(event);

        for each(var argument:String in invocation.arguments)
        airParams.addItem(argument);
    }

另外:  allowBrowserInvocation设置为true

不要做其他事情,需要在AIR启动时将一些变量从flex传递给AIR(onInvoke事件),但它没有显示任何想法?

0 个答案:

没有答案