Flex的RIA应用程序托盘和QQ提醒功能
托盘功能,是应用程序在最小化或关闭时,隐藏窗口,将应用程序显示在托盘处,不显示在任务栏中.还有类似于QQ提醒功能的实现.
AIR程序的WindowApplication文件的mxml;
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApplication()"> <mx:Style source="css/CSS.css"/> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.core.UIComponent; import mx.core.Container; import mx.events.ItemClickEvent; import mx.events.CloseEvent; import flash.display.BitmapData; import flash.errors.*; import flash.events.*; import flash.external.*; import flash.desktop.NativeApplication; private var dockImage:BitmapData; //初始化Application里调用此方法,完成上面的第一步: public function initApplication():void{ var loader:Loader=new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,prepareForSystray);//这里就是完成第一步的任务须,这个prepareForSystray就是对托盘的生在和菜单的控制 loader.load(new URLRequest("png/systray_icon_16.png"));//这里先要加载托盘图标的小图片 this.addEventListener(Event.CLOSING,closingApplication);//设置关闭体的事件 } //关闭窗体的事件 public function closingApplication(event:Event):void{ event.preventDefault();//阻止默认的事件 Alert.yesLabel="关闭"; Alert.noLabel="最小化"; Alert.show("关闭 还是 最小化?", "是否需要关闭?", 3, this, alertCloseHandler);//弹出自定义的选择框, 关于Alert的详细用法,参考官方文档或我前面的相关文章. } //根据用户的选择来判断做什么,这里选择是就是关闭,选择否(Mini)就是最小化到托盘. private function alertCloseHandler(event:CloseEvent):void{ if(event.detail==Alert.YES){ closeApp(event); }else{ dock();//最小化到托盘 } } //生成托盘 public function prepareForSystray(event:Event):void{ dockImage=event.target.content.bitmapData; if(NativeApplication.supportsSystemTrayIcon){ setSystemTrayProperties();//设置托盘菜单的事件 SystemTrayIcon(NativeApplication.nativeApplication.icon).menu=createSystrayRootMenu();//生成托盘菜单 } } public function createSystrayRootMenu():NativeMenu{ var menu:NativeMenu = new NativeMenu(); var openNativeMenuItem:NativeMenuItem = new NativeMenuItem("Open");//生成OPEN菜单项 var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("Exit");//同理 openNativeMenuItem.addEventListener(Event.SELECT, undock); exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);//添加EXIT菜单项事件 menu.addItem(openNativeMenuItem); menu.addItem(new NativeMenuItem("",true));//separator menu.addItem(exitNativeMenuItem);//将菜单项加入菜单 return menu; } //设置托盘图标的事件 private function setSystemTrayProperties():void{ SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = "订单提醒"; SystemTrayIcon(NativeApplication.nativeApplication.icon).addEventListener(MouseEvent.CLICK, undock); stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized); } //最小化窗体 private function nwMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void { if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED) { displayStateEvent.preventDefault();//阻止系统默认的关闭窗体事件 dock();//将程序放入托盘 } } //将本地应用程序放到托盘 public function dock():void { stage.nativeWindow.visible = false; //设置本地程序窗体不可见 NativeApplication.nativeApplication.icon.bitmaps = [dockImage];//设置托盘的图标 } //激活程序窗体 public function undock(evt:Event):void { stage.nativeWindow.visible = true;//设置本地程序窗体可见 stage.nativeWindow.orderToFront();//设置本地程序窗体到最前端 NativeApplication.nativeApplication .icon.bitmaps = [];//将托盘图标清空 } //关闭程序窗体 private function closeApp(evt:Event):void { stage.nativeWindow.close(); } public function about():void{ Alert.okLabel="知道了"; Alert.show("JODY信息有限公司\nJODY信息有限公司,荣誉出品。"); } public function infos():void{ var popUp:PopMsg=new PopMsg(); popUp.systemChrome=NativeWindowSystemChrome.NONE; popUp.transparent=true; popUp.showTitleBar=false; popUp.showStatusBar=false; popUp.open(); popUp.init(); } public function getOrderInfos():void{ srv.send(); infos(); } ]]> </mx:Script> <mx:ApplicationControlBar width="98%" top="0" horizontalCenter="1" height="59" fillAlphas="[1.0, 1.0]" fillColors="[#2F58F1, #2F58F1]" color="#FBFDFE" verticalAlign="middle"> <mx:ControlBar height="49" width="100%" horizontalAlign="left" verticalAlign="middle"> <mx:Image source="png/logoicon.png" autoLoad="true" width="32" height="32"/> <mx:Label text="订单客户端" fontWeight="bold" fontFamily="Verdana" fontSize="14"/> <mx:Spacer width="100%"/> <mx:Button id="f5order" label="刷新订单" cornerRadius="12" color="0xffffff" fillColors="[0x55C0FF,0x0050AA]" fillAlphas="[1.0,1.0]" highlightAlphas="[1.0,0.2]" focusAlpha="0.2" textRollOverColor="0xffffff" textSelectedColor="0x55C0FF" click="getOrderInfos()" labelPlacement="right"/> <mx:Button id="aboutinfo" label="关于系统" cornerRadius="12" color="0xffffff" fillColors="[0x55C0FF,0x0050AA]" fillAlphas="[1.0,1.0]" highlightAlphas="[1.0,0.2]" focusAlpha="0.2" textRollOverColor="0xffffff" textSelectedColor="0x55C0FF" click="about()" labelPlacement="right"/> </mx:ControlBar> </mx:ApplicationControlBar> <mx:HTTPService id="srv" url="order.xml"/> <mx:Panel width="98%" height="90%" layout="absolute" horizontalCenter="1" top="67" title="订单信息"> <mx:DataGrid dataProvider="{srv.lastResult.orders.order}" width="100%" horizontalCenter="0" top="0" height="80%"> <mx:columns> <mx:DataGridColumn headerText="序号" dataField="id"/> <mx:DataGridColumn headerText="订单号" dataField="orderId"/> <mx:DataGridColumn headerText="订单来源" dataField="orderfor"/> <mx:DataGridColumn headerText="订单金额" dataField="amout"/> <mx:DataGridColumn headerText="订单状态" dataField="status"/> </mx:columns> </mx:DataGrid> </mx:Panel> </mx:WindowedApplication>
下面是模式的HttpService请求的order.xml
<?xml version="1.0" encoding="utf-8"?>
<orders>
<order orderId="D10001">
<id>1</id>
<orderfor>Web商城</orderfor>
<amout>210</amout>
<status>未受理</status>
</order>
<order orderId="D10002">
<id>2</id>
<orderfor>Wap商城</orderfor>
<amout>310</amout>
<status>未受理</status>
</order>
<order orderId="D10003">
<id>3</id>
<orderfor>SMS商城</orderfor>
<amout>410</amout>
<status>未受理</status>
</order>
</orders>
QQ提醒功能的PopMsg.mxml
<?xml version="1.0" encoding="utf-8"?> <mx:Window xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="200"> <mx:Script> <![CDATA[ import mx.controls.Alert; //定时器淡入 private var Timer1:Timer=new Timer(1); //停留时间 private var Timer2:Timer=new Timer(8000,1); //定时器淡出 private var Timer3:Timer=new Timer(1); private var _x:uint=0; private var _y:uint=0; private var _height:uint=0; private var screenBounds:Rectangle; public function init():void{ this.startPosition(); this.Timer1.addEventListener(TimerEvent.TIMER,scorllUp); //this.colseBtn.addEventListener(MouseEvent.CLICK,closeApp); this.Timer1.start(); } /** *弹出窗口 */ private function scorllUp(e:TimerEvent):void{ if(this._height<200){ this._height++; this.setLocation(this._x,this._y-30-this._height); } else{ this.Timer2.addEventListener(TimerEvent.TIMER,wait); this.Timer2.start(); this.Timer1.stop(); } } /* *设置y坐标 * */ private function setLocation(x:uint,y:uint):void{ this.nativeWindow.y=y; this.nativeWindow.x=x; //trace(x+'='+y); } private function wait(e:TimerEvent):void{ this.Timer3.addEventListener(TimerEvent.TIMER,scorllDown); this.Timer3.start(); } private function scorllDown(e:TimerEvent):void{ try{ if(this.Timer2!=null){ this.Timer2.stop(); this.Timer2=null; } if(this._height>0){ this._height--; //trace(this._height); this.setLocation(this._x,this.nativeWindow.y+1); //trace(this.nativeWindow.y); } else{ this.Timer3.stop(); this.close(); } }catch(error:Error){ Alert.show("运行出现异常"+error.message); } } /* *初始位置 */ public function startPosition():void{ this.screenBounds=Screen.mainScreen.bounds; //trace(this.screenBounds); this._x=this.screenBounds.width- 300; //trace(this._x); this._y=this.screenBounds.height; //trace(this._y); this.nativeWindow.x=this._x; this.nativeWindow.y=this._y; } private function closeApp(e:MouseEvent):void{ this.close(); } public function closes():void{ this.close(); } ]]> </mx:Script> <mx:TitleWindow title="系统消息" titleIcon="@Embed(source='png/systray_icon_16.png')" width="100%" height="100%" layout="absolute"> <mx:Label text="您有新的订单,请点击查看。\n请及时受理订单。" width="179" height="111"/> </mx:TitleWindow> </mx:Window>