AIR程序多开

AIR应用通常不能像QQ那样能进行多开操作。

 

为了让一个用AIR做的客户端能实现多任务,我找到得办法是运行程序时自动修改配置文件的id标签内的内容。

 

然后再关闭程序时又必须还原成原有的id,因为只有id一致才能实现更新功能。

 

 

主程序:multiapp.mxml


    <?xml version="1.0" encoding="utf-8"?>  
    <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"  
                           creationComplete="windowedapplication1_creationCompleteHandler(event)">  
        <fx:Script>  
            <![CDATA[  
                import flash.filesystem.File;  
                import flash.filesystem.FileMode;  
                import flash.filesystem.FileStream;  
                  
                import mx.events.FlexEvent;  
                import mx.formatters.DateFormatter;  
                  
                private var oldid:String;  
                private var reg:RegExp = /<id>.*<\/id>/;  
                  
                protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void  
                {  
                    var df:DateFormatter = new DateFormatter();  
                    df.formatString = "YYYYMMDDHHNNSS";  
                    var time:String = df.format(new Date());  
                    var newid:String = "<id>com.roy"+time+"</id>";  
                      
                    var f:File =new File(File.applicationDirectory.resolvePath(  
                        "META-INF/AIR/application.xml").nativePath);  
                    var fs:FileStream = new FileStream();    
                    fs.open(f,FileMode.READ);        
                    var str:String = new String(fs.readUTFBytes(fs.bytesAvailable));  
                    oldid = str.match(reg).toString();  
                    str = str.replace(reg,newid);    
                    fs.open(f,FileMode.WRITE);  
                    fs.writeUTFBytes(str);   
                    fs.close();  
                      
                    label.text = newid;  
                      
                    //有多个在运行的程序时只关闭一个运行程序就无法再打开新程序  
                    //必须监听系统最后一个相同程序关闭时,才能初始化id  
                    //解决办法:  
                    //记录运行前id,若为初始ID则在关闭该程序时初始化id,否则不操作  
                      
                    if(oldid == "<id>com.roy</id>")  
                    {  
                        this.addEventListener(Event.CLOSE,returnId);  
                    }  
                }  
                  
                protected function returnId(e:Event):void  
                {  
                    var f:File =new File(File.applicationDirectory.resolvePath(  
                        "META-INF/AIR/application.xml").nativePath);  
                    var fs:FileStream = new FileStream();    
                    fs.open(f,FileMode.READ);        
                    var str:String = new String(fs.readUTFBytes(fs.bytesAvailable));  
                    str = str.replace(reg,oldid);    
                    fs.open(f,FileMode.WRITE);  
                    fs.writeUTFBytes(str);   
                    fs.close();  
                }  
                  
            ]]>  
        </fx:Script>  
          
        <s:VGroup gap="20">  
            <s:Label text="已将配置文件id更改为:" color="0xff0000" fontSize="20"/>  
            <s:Label id="label" color="0x0000ff" fontSize="20"/>  
        </s:VGroup>  
    </s:WindowedApplication>  

此程序中配置文件multiapp-app.mxml中,id标签必须为<id>com.roy</id>。


转载:http://mad-roy.iteye.com/blog/1225788

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值