开发者

Air Application - Start in system tray?

I'd be extremely grateful if somebody could help me, or point me in the right direction.

I've been trying to get an adobe air application start in system tray, so far I've used this snippet: http://www.swamicharan.com/blog/air/minimizing-an-air-app-to-systemtray/ which works as described, however no matter what I do I can't seem to make it start, minimized, in the system tray. This is the code I have so far:

<?xml version="1.0" encoding="utf-8"?>
<mx: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="300" height="100" creationComplete="initApp()" layout="horizontal">
    <fx:Script>
        <![CDATA[
            import mx.events.CloseEvent;

            private var trayIcon:BitmapData;

            public function initApp():void{
                loadTrayIcon();

                this.addEventListener(Event.CLOSING, minToTray);
            }

            public function loadTrayIcon():void{
                var lo开发者_开发问答ader:Loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, readyToTray);
                loader.load(new URLRequest("assets/icon.PNG"));
            }

            private function minToTray(event:Event):void{
                event.preventDefault();
                dock();
            }

            public function readyToTray(event:Event):void{
                trayIcon = event.target.content.bitmapData;

                var myMenu:NativeMenu = new NativeMenu();

                var openItem:NativeMenuItem = new NativeMenuItem("Options");
                var closeItem:NativeMenuItem = new NativeMenuItem("Close");

                openItem.addEventListener(Event.SELECT, unDock);
                closeItem.addEventListener(Event.SELECT, closeApp);

                myMenu.addItem(openItem);
                myMenu.addItem(new NativeMenuItem("", true));
                myMenu.addItem(closeItem);

                if(NativeApplication.supportsSystemTrayIcon){
                    SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = "Notifier";

                    SystemTrayIcon(NativeApplication.nativeApplication.icon).
                        addEventListener(MouseEvent.CLICK, unDock);

                    stage.nativeWindow.addEventListener(
                        NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, winMinimized);

                    SystemTrayIcon(NativeApplication.nativeApplication.icon).menu = myMenu;
                }
            }

            private function winMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void{
                if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED){
                    displayStateEvent.preventDefault();
                    dock();
                }
            }

            public function dock():void{
                stage.nativeWindow.visible = false;

                NativeApplication.nativeApplication.icon.bitmaps = [trayIcon];
            }

            public function unDock(event:Event):void{
                stage.nativeWindow.visible = true;
                stage.nativeWindow.orderToFront();

                NativeApplication.nativeApplication.icon.bitmaps = [];
            }

            private function closeApp(event:Event):void{
                stage.nativeWindow.close();
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Style> 
        @namespace s "library://ns.adobe.com/flex/spark"; 
        s|WindowedApplication 
        { 

            skinClass:ClassReference("spark.skins.spark.SparkChromeWindowedApplicationSkin"); 
            background-color:#999999; 
            background-alpha:"0.7"; 
        }          
    </fx:Style>

    <s:Label text="Hello AIR"/> 
</mx:WindowedApplication>

Many Thanks.


I think you'll manage by calling dock() at the end of readyToTray(event:Event). To make sure your initialWindow is invisible when it launches you can set it's visible property to false in the application descriptor file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜