类别 AIR 订阅所有【AIR】的日志

更新AIR应用程序

关于AIR应用程序的更新,推荐从浏览器中(使用无缝安装功能)安装或更新。不过,也可以使用Updater类让安装的应用程序自行更新到新版本。使用Updater类的 update()方法,可以指向用户计算机上的 AIR 文件,并更新为该版本。

首先,将最新版的air文件及其描述文件放置在服务器上。示例文件如下:

<updates>
    <update location="http://localhost/update/update.air" version="2.0">
</update>

然后加载描述文件并读取AIR应用程序的最新版本号,再与当前应用描述符中的版本号比较,如果不匹配则加载AIR应用程序最新版更新。如下代码:

import flash.desktop.*;
import flash.filesystem.*;
import flash.net.*;
import flash.events.MouseEvent;

checkForUpdate();
var updateURL:String;
var updateVersion:String;
var stream:URLStream;
var fileData:ByteArray;
var updateFile:File;
updateButton.enabled=false;
updateButton.addEventListener(MouseEvent.CLICK, startUpdate);

function checkForUpdate():void {
	var req:URLRequest=new URLRequest("http://localhost/update/update.xml");
	var loader:URLLoader = new URLLoader();
	loader.addEventListener( Event.COMPLETE, onXMLLoadComplete );
	loader.load(req);
}

function onXMLLoadComplete( event:Event ):void {
	var loader:URLLoader=event.target as URLLoader;
	var xmlData:XML=XML(loader.data);
	updateVersion=xmlData.update[0].@version;
	updateURL=xmlData.update[0].@location;
	checkStatus();
}

function checkStatus():void {
	var appXML:XML=NativeApplication.nativeApplication.applicationDescriptor;
	var air:Namespace=appXML.namespaceDeclarations()[0];
	var version:String=appXML.air::versionNumber;
	if (version!=updateVersion) {
		updateButton.enabled=true;
	}
}

function startUpdate(event:MouseEvent):void {
	var req:URLRequest=new URLRequest(updateURL);
	stream = new URLStream();
	fileData = new ByteArray();
	stream.addEventListener( Event.COMPLETE, onDownloadComplete );
	stream.load( req );
}

function onDownloadComplete( event:Event ):void {
	stream.readBytes( fileData, 0, stream.bytesAvailable );
	updateFile=File.desktopDirectory.resolvePath("update.air");
	var fs:FileStream = new FileStream();
	fs.addEventListener( Event.CLOSE, performUpdate );
	fs.openAsync( updateFile, FileMode.WRITE );
	fs.writeBytes( fileData, 0, fileData.bytesAvailable );
	fs.close();
}

function performUpdate( event:Event ):void {
	var updater:Updater= new Updater();
	updater.update(updateFile, updateVersion);
}

当然,运行时将检查在AIR文件中指定的应用程序ID和发布者ID是否与调用 update() 方法的应用程序的应用程序ID和发布者ID相匹配,不匹配肯定更新不成功。另外,可能还需要在Web服务器上设置air文件的mime类型:application/vnd.adobe.air-application-installer-package+zip。

上一篇上一篇: 使用NativeMenu制作AIR多级上下文菜单
下一篇下一篇: Flex Finance Chart
来自文章来自: 本站原创
引用引用通告: 查看所有引用 | 我要引用此文章
标签Tags: TestDrive
相关相关日志:
评论: 1 | 引用: 0 | 查看次数: -
引用kokko[2009-01-14 12:26 AM | 无网站 | 无Mail | 123.112.77.100 | 删除 | 取消审核 | 回复回复]
[正在加载评论信息,请稍候...]
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭