AIR 1.5 加入了自动升级的类air.update.ApplicationUpdater,为基于AIR的应用程序能通过网络更新到最新版本有了保障。如果发布自己的AIR程序建议加上自动升级功能,好处当然不用多说。

其原理也很简单,ApplicationUpdater 会去读取网络上的一个版本描述文件,描述文件事是一个特定的XML文档,其中包含了版本号和对应的安装文件路径以及更新描述信息。然后和当前运行的AIR程序版本进行比较,来决定是否下载和安装。

整理了一下代码,在以后的AIR程序中只要简单的引入这个类文件即可实现检查新版本和安装新版本的AIR程序。

这里来测试,注意标题栏的版本号,升级成功后为2.0

Please upgrade your Flash Player This is the content that would be shown if the user does not have Flash Player 9.0.115 or higher installed.

直接下载

版本描述文件

 
<?xml version="1.0" encoding="utf-8"?> 
	<!-- update.xml -->
     <update xmlns="http://ns.adobe.com/air/framework/update/description/1.0"> 
       <version>2.0</version> 
       <url>http://www.cbmland.com/air/AutoUpdate/AutoUpdate.air</url>
       <description> 
           <text xml:lang="en_US">AIR AutoUpdate</text>
           <text xml:lang="zh_CN">AIR 自动更新</text>
       </description> 
    </update>

UpdateUtil.as

package com.cbmland.utils
{
	import air.update.ApplicationUpdater;
	import air.update.events.DownloadErrorEvent;
	import air.update.events.StatusUpdateEvent;
	import air.update.events.UpdateEvent;
	import flash.events.ErrorEvent;
	import flash.events.Event;
 
 
	public class UpdateUtil
	{
		private var au:ApplicationUpdater;
		/**
		 * 
		 * @param updateURL 版本描述文件URL
		 * @param eventHandle 事件挂钩
		 * 
		 */		
		public function UpdateUtil(updateURL:String,eventHandle:Function = null){
 
				au = new ApplicationUpdater();
 
				//设置版本描述文件URL
				this.au.updateURL = updateURL;
 
				//如果有事件挂钩便进行事件监听
				if(eventHandle){
					this.au.addEventListener(ErrorEvent.ERROR,eventHandle)
					this.au.addEventListener(UpdateEvent.CHECK_FOR_UPDATE,eventHandle)
					this.au.addEventListener(UpdateEvent.INITIALIZED,eventHandle)
					this.au.addEventListener(UpdateEvent.DOWNLOAD_START,eventHandle)
					this.au.addEventListener(UpdateEvent.DOWNLOAD_COMPLETE,eventHandle)
					this.au.addEventListener(UpdateEvent.BEFORE_INSTALL,eventHandle)
					this.au.addEventListener(StatusUpdateEvent.UPDATE_STATUS,eventHandle)
					this.au.addEventListener(DownloadErrorEvent.DOWNLOAD_ERROR,eventHandle)
				}
				//初始化
				this.au.initialize()
				//开始检查
				this.au.checkNow()
 
		}
 
	}
}

AutoUpdate.as

package {
 
	import com.cbmland.utils.UpdateUtil;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.StatusEvent;
	import flash.text.TextField;
 
	public class AutoUpdate extends Sprite{
 
		private var console:TextField;
 
		public function AutoUpdate()
		{
			initConsole()
 
			new UpdateUtil('http://www.cbmland.com/air/AutoUpdate/update.xml',eventHandle)
		}
		private function initConsole(){
 
			console = new TextField()
 
			console.width = this.stage.stageWidth;
			console.height = this.stage.stageHeight;
			console.border = false;
			console.multiline = true;
			console.wordWrap = true;
			this.addChild(console)
 
		}
		private function eventHandle(event:Event){
 
			console.appendText(event+'\n');
 
			if(event is StatusEvent){
 
				doUpdate(event)
			}
		}
		private function doUpdate(event:Event):void{
 
			if(event.available==true){
 
				event.preventDefault()
				au.downloadUpdate()
			}
		}
	}
}

源码下载

Download: AutoUpdate.zip  AutoUpdate.zip (9.0 KiB, 870 hits)
AIR 程序自动更新测试

有几个需要注意的不得不提醒一下。
需要Flex SDK 3.2才支持AIR 1.5

XML中的版本号和指定的AIR程序的版本号一定要保证匹配,比如1.0千万不要写成了1.00了。
如果在ADL中看到报错[ErrorEvent type="error" bubbles=false cancelable=false eventPhase=2 text="Cannot update (from remote)" errorID=16828]
请使用发行版本来测试,调试版本不支持升级安装。