实现AIR应用自动更新的大致原理(冰山上的播客)

实现AIR应用自动更新的大致原理,以下是类文件和调用示例:

package Neo
{
import flash.display.*;
import flash.filesystem.*;
import flash.desktop.Updater;
import flash.events.*;
import flash.net.*;
import flash.xml.*;
import flash.text.TextField;
import flash.utils.ByteArray;
import flash.events.IOErrorEvent;

public class AIRUpdater extends Sprite
{
public var version:String;
public var updataURL:String;

private var updataRequest:URLRequest;
private var updataLoader=new URLLoader();
private var newVersion:String;
private var airURLString:String;
private var urlReq:URLRequest;
private var urlStream:URLStream;
private var fileData:ByteArray;
private var baseSprite:Sprite=new Sprite();
private var tipText:TextField=new TextField();
private var btYes:Sprite=new Sprite();
private var btYesText:TextField=new TextField();
private var btNo:Sprite=new Sprite();
private var btNoText:TextField=new TextField();

public function AIRUpdater():void
{
baseSprite.graphics.beginFill(0×000000,0.5);
baseSprite.graphics.lineStyle(8,0×000000,0.1);
baseSprite.graphics.drawRoundRect(0,0,360,160,10);
baseSprite.graphics.endFill();
/*add base*/
tipText.width=300;
tipText.height=30;
tipText.x=tipText.y=30;
tipText.autoSize=”center”;
tipText.textColor=0xFFFFFF;
tipText.text=”You must update your AIR Application now,ok?”;
baseSprite.addChild(tipText);
/*add button*/
btYes.graphics.beginFill(0×000000,0.5);
btYes.graphics.lineStyle(1,0×000000,0.1);
btYes.graphics.drawRoundRect(0,0,36,20,10);
btYes.graphics.endFill();
btYesText.width=36;
btYesText.text=”Yes”;
btYesText.autoSize=”center”;
btYesText.textColor=0xFFFFFF;
btYes.addChild(btYesText);
btNo.graphics.beginFill(0×000000,0.5);
btNo.graphics.lineStyle(1,0×000000,0.1);
btNo.graphics.drawRoundRect(0,0,36,20,10);
btNo.graphics.endFill();
btNoText.width=36;
btNoText.text=”No”;
btNoText.autoSize=”center”;
btNoText.textColor=0xFFFFFF;
btNoText.selectable=btYesText.selectable=false;
btNo.addChild(btNoText);
btYes.x=100;
btYes.y=100;
btNo.x=210;
btNo.y=100;
btYes.buttonMode=btNo.buttonMode=true;
baseSprite.addChild(btYes);
baseSprite.addChild(btNo);
}
/*Function For Update Application*/
public function updateMyApp():void
{
updataRequest=new URLRequest(updataURL);
updataLoader.load(updataRequest);
updataLoader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
updataLoader.addEventListener(Event.COMPLETE,loaderCompleteHandler);
}
/*loaderCompleteHandler*/
private function loaderCompleteHandler(event:Event):void
{
try {
var result:XML=new XML(updataLoader.data);
if (version != result.version)
{
newVersion=result.version;
airURLString=result.lastpublic;
trace(”马上更新到最新版吗?” + “有新的更新”);
addChild(baseSprite);
baseSprite.x=(stage.stageWidth-baseSprite.width)/2;
baseSprite.y=(stage.stageHeight-baseSprite.height)/2;
btYes.addEventListener(MouseEvent.CLICK,updateBtHandler);
btNo.addEventListener(MouseEvent.CLICK,removeBtHandler);
}
} catch (e:TypeError) {
trace(”Updata Application Error:Could not parse the XML file.”);
}
}
/*errorHandler*/
private function errorHandler(e:IOErrorEvent):void
{
trace(”Updata Application Error:Had problem loading the XML File.”);
}
private function updateBtHandler(event:MouseEvent):void
{
urlReq=new URLRequest(airURLString);
urlStream=new URLStream ;
fileData=new ByteArray ;
urlStream.addEventListener(Event.COMPLETE,loaded);
urlStream.load(urlReq);
}
private function removeBtHandler(event:MouseEvent):void
{
removeChild(baseSprite);
}
private function loaded(event:Event):void
{
urlStream.readBytes(fileData,0,urlStream.bytesAvailable);
writeAirFile();
}
private function writeAirFile():void
{
var file:File=File.desktopDirectory.resolvePath(”lastPublish.air”);
var fileStream:FileStream=new FileStream ;
fileStream.addEventListener(Event.CLOSE,fileClosed);
fileStream.openAsync(file,FileMode.WRITE);
fileStream.writeBytes(fileData,0,fileData.length);
fileStream.close();
}
private function fileClosed(event:Event):void
{
trace(”The AIR file is written.”);
var updater:Updater=new Updater ;
var airFile:File=File.desktopDirectory.resolvePath(”lastPublish.air”);
updater.update(airFile,newVersion);
}
}
}

调用示例:
import flash.display.*;
import flash.filesystem.*;
import flash.desktop.Updater;
import flash.events.*;
import flash.net.*;
import flash.xml.*;
import flash.utils.ByteArray;
import flash.events.IOErrorEvent;
import Neo.AIRUpdater;

var aupSample:AIRUpdater=new AIRUpdater();
aupSample.version=”alpha1″;
aupSample.updataURL=”http://localhost/testUpdate/version.xml”;
aupSample.updateMyApp();
addChild(aupSample);
用于判断版本的XML结构示意:


alpha2
http://localhost/testUpdate/main.air

你可能感兴趣的:(Flex)