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>。